Ruby on Rails显示f.text_area剩余的字符数

时间:2015-03-12 18:42:13

标签: javascript ruby-on-rails ruby character charactercount

在我的Ruby on Rails应用程序中,我试图在视图中显示以下文本框剩余的字符数量:

<%= f.label :review_text, 'Review Text:' %>

我正试图通过这个java来实现:

$(document).ready(function() {
  $("#review_text").keyup(function() {
   $("#counter").text(2000 - $(this).val().length);
  });
});

显示剩下的金额:

chars left : <span id="counter"><%= maxcounter=2000 %></span>

这不起作用,但此文本字段确实:

<%= text_field_tag :review_text %>

如果我将上述内容更改为:

<%= f.text_field_tag :review_text %>

我收到以下错误:

NoMethodError in Reviews#new 
undefined method `text_field_tag' for   <ActionView::Helpers::FormBuilder:0x6b44020>

有人可以帮忙。

以下是完整视图:

<%= stylesheet_link_tag    "application", media: "all", "data-turbolinks-track" => true %>
<div id="contentWrapper">
    <div id="content">
    <div class='large_panel'>
        <table>
            <%= form_for @review do |f| %>
                <%= f.hidden_field :user_id %>
                <%= f.hidden_field :film_id %>
                <%= image_tag "thor_hammer.jpg",:size => "900x250" %>
                <h1>REVIEW <%= @review.film.title %>:</h1>
                <tr>
                    <td width="500px">
                        <%= f.label :review_text, 'Review Text:' %></br>
                    </td>
                </tr>
                <tr>
                    <td>
                        <%= f.text_area :review_text, :size=>"50x10" %></br></br>
                    </td>
                </tr>
                <tr>
                <td width="100px">
                        <%= f.label :no_of_stars, 'Stars:' %></br>
                    </td>
                </tr>
                <tr>
                    <td>
                        <%= f.select :no_of_stars, 1..5 %></br></br>
                    </td>
                </tr>
                <tr>
                    <td>
                    chars left : <span id="counter"><%= maxcounter=2000 %></span>
                    <%= text_field_tag :review_text %>
                        <div class="actions">
                          <%= f.submit 'Submit' %>
                        </div>
                        <div class="actions">
                          <%= link_to 'Back', reviews_path %>
                        </div>
                        </br></br>
                        <%= render "/error_messages", :message_header => "Cannot save: ", :target => @review %> 
                    </td>
                </tr>
            <% end %>
            </table><br>
        </div>
    </div>
</div>

<script>
$(document).ready(function() {
  $("#review_text").keyup(function() {
    $("#counter").text(2000 - $(this).val().length);
  });
});
</script>

2 个答案:

答案 0 :(得分:5)

经过测试和工作:

reviews_controller.rb:

class ReviewsController < ApplicationController

    def new
        @review = Review.new
        @maximum_length = Review.validators_on( :review_text ).first.options[:maximum]
    end

end

review.rb:

class Review < ActiveRecord::Base

    validates :review_text, length: { maximum: 200 }

end

new.html.erb:

<%= form_for @review do |f| %>

    <%= f.text_field :review_text, maxlength: @maximum_length, id: 'review_text' %>

    Chars left: <span id="counter" data-maximum-length="<%= @maximum_length %>"><%= @maximum_length %></span>

<% end %>


<script>
    $(document).ready(function() {
      var review_text = $("#review_text");
      var counter     = $("#counter");
      var max_length  = counter.data("maximum-length");

      review_text.keyup(function() {
          counter.text(max_length - $(this).val().length);
      });

    });
</script>

答案 1 :(得分:0)

接受的答案按照提及的方式工作,但是如果您的text_field预先填充了数据,那么当页面加载时,计数器将不包含正确的计数,要解决此问题,请在上面添加以下行{ {1}}

review_text.keyup()

或者只使用以下更新的脚本:

counter.text(max_length - review_text.val().length);