如何将Javascript添加到simple_form?

时间:2012-12-08 17:37:33

标签: javascript ruby-on-rails ruby simple-form

我正在使用带有Rails 3.2.5的simple_form。我的表格看起来像这样:

<%= simple_form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => {:class => 'form-vertical' }, :html => {:name => "new_user"}) do |f| %>
    <%= f.error_notification %>
    <%= f.input :field_one,
                :placeholder => '30000',
            :autofocus => true,
            :required => true,
            :html => {:onFocus => 'startCalc();'},
            :html => {:onBlur => 'stopCalc();'} %>
# More form fields

<% end %>

当用户在表单字段中输入数字时,我正在尝试调用Javascript驱动的计算,但Javascript未显示在输出的HMTL中:

<input autofocus="autofocus" class="numeric decimal required" id="field_one" name="user[field_one]" placeholder="30000" step="any" type="number" />

有谁知道如何让simple_form添加这些Javascript?

谢谢!

格雷格

1 个答案:

答案 0 :(得分:3)

我现在看到了我的错误。根据{{​​3}},我需要使用:

:input_html => {:onFocus => 'startCalc();'},
:input_html => {:onBlur => 'stopCalc();'}

这有效!