Rails3和jQuery - 绑定这个javascript

时间:2011-05-26 00:18:25

标签: jquery ruby-on-rails-3

使用Rails3并安装了最新的jQuery插件。我试图获得char。 count函数使用下面的代码,但没有任何成功。任何帮助将不胜感激??

物品/ _form.html.erb

<div class="field">
<%= f.label :title %><br />
<%= f.text_field :title %>
</div>

<div class="field">
<%= f.label :body %><br />
<%= f.text_area :body id=testTextarea2 %>
</div>

<div class="field">
<%= f.label :tag_names, "Tags" %>  <br />
<%= f.text_field :tag_names %> 
</div>

布局/ application.html.erb

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
 <title>myblog.com</title>
   <%= stylesheet_link_tag 'scaffold' %>
   <%= stylesheet_link_tag 'screen', :media => 'screen' %>
   <%= stylesheet_link_tag 'print',  :media => 'print' %>
   <%= javascript_include_tag :defaults, "jquery.js", "counter.js", "rails.validations" %>
   <%= csrf_meta_tag %>
   <script src="/javascripts/jquery.textareaCounter.plugin.js" type="text/javascript"></script>


<script type="text/javascript">
var info;
$(document).ready(function()    {

       var options = {  
        'maxCharacterSize': -2,
        'originalStyle': 'originalTextareaInfo',
        'warningStyle' : 'warningTextareaInfo',
        'warningNumber': 40
        };
        $('#testTextarea').textareaCount(options);


       var options2 = {
        'maxCharacterSize': 200,
        'originalStyle': 'originalTextareaInfo',
        'warningStyle' : 'warningTextareaInfo',
        'warningNumber': 40,
        'displayFormat' : '#input/#max | #words words'
        };
        $('#testTextarea2').textareaCount(options2);


       var options3 = {
        'maxCharacterSize': 200,
        'originalStyle': 'originalTextareaInfo',
        'warningStyle' : 'warningTextareaInfo',
        'warningNumber': 40,
        'displayFormat' : '#left Characters Left / #max'
        };
        $('#testTextarea3').textareaCount(options3, function(data)

                    {
        $('#showData').html(data.input + " characters input. <br />" + data.left + " characters left. <br />" + data.max + " max characters. <br />" + data.words + " words input.");
            });
        });
</script>

SyntaxError in Articles#new

Showing /Users/blog/app/views/articles/_form.html.erb where line #23 raised:

compile error
/Users/blog/app/views/articles/_form.html.erb:23: syntax error, unexpected tIDENTIFIER,     expecting ')'
...append= ( f.text_area :body id="testTextarea2" );@output_buf...
                          ^
Extracted source (around line #23):

20: 
21:   <div class="field">
22:     <%= f.label :body %><br />
23:     <%= f.text_area :body id="testTextarea2" %>
24:   </div>
25:  

2 个答案:

答案 0 :(得分:1)

您看到的错误与jQuery或JS无关。 text_area帮助器需要一个属性哈希 - 这应该可以解决错误:

f.text_area :body, :id => "testTextarea2"

答案 1 :(得分:0)

我希望看到以下html在某处生成,它在哪里 - 你没有显示该代码吗?

 <textarea id="testTextarea" cols="60" rows="10" ></textarea>
 <div class="originalTextareaInfo warningTextareaInfo">

 <textarea id="testTextarea2" cols="60" rows="10" ></textarea>
 <div class="originalTextareaInfo warningTextareaInfo">

 <textarea id="testTextarea3" cols="60" rows="10" ></textarea>
 <div class="originalTextareaInfo warningTextareaInfo">
 <div id="showData"></div>