【问题标题】:How to get data from input field using ruby on rails如何使用 ruby​​ on rails 从输入字段中获取数据
【发布时间】:2011-04-29 04:35:56
【问题描述】:

如何从 HTML 文本字段中获取数据并在 RoR 中使用?

例如,假设我在文本字段中输入了一个单词,然后 RoR 打印出该单词中有多少个字符。

【问题讨论】:

    标签: ruby-on-rails input textfield


    【解决方案1】:

    最好的方法是使用 JAVASCRIPT,但你坚持 ROR 使用 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" %>
    

    在控制器中

    def word_length
      word = params[:word]
      render :update do |page|
        page.replace_html  'word_message', "#{word.length} characters"
      end
    end
    

    【讨论】:

    • 感谢您的回复。但是我得到:#:0x3eec950> Rails 3.0.7 Ruby 1.9.2 的未定义方法`observe_field'
    猜你喜欢
    • 1970-01-01
    • 2010-11-11
    • 2018-11-18
    • 2013-04-08
    • 2016-07-29
    • 2014-01-04
    • 1970-01-01
    • 2013-09-06
    • 1970-01-01
    相关资源
    最近更新 更多