How to get data from input field using ruby on rails
How to get data from an HTML text field and use it in RoR?
For example, let's say I enter a word in t开发者_StackOverflow中文版he text field and RoR prints how many characters are in said word.
Best way to do that is using the JAVASCRIPT but you insist for ROR use observe_field
<%= text_field :users, :word, :value=>'', :autocomplete=>'off' %><br>
<span id="word_message" style="color :red; font-size: 9px;"></span>
<%= observe_field :users_word, :url => { :action => :word_length, :id=>1 },
:on=>"blur",
:frequency => 0.25,
:with => "'word='+value" %>
In Controller
def word_length
word = params[:word]
render :update do |page|
page.replace_html 'word_message', "#{word.length} characters"
end
end
精彩评论