Rails 简单表单按钮id

Rails Simple form button id

我无法让 id 在按钮上以简单的形式工作,我试过了

this

= f.button, :input_html => { :id => 'my_id' }
= f.button, :input_html => { :id => 'my_id' }, :submit

and this

= f.button, html: { id: :edit_account } 
= f.button, html: { id: :edit_account } :submit

正确的做法是什么?

试试看 = f.button :submit, input_html: { id: 'edit_account' }

来自文档:

If you want to pass the same options to all inputs in the form (for example, a default class), you can use the :defaults option in simple_form_for. Specific options in input call will overwrite the defaults:

<%= simple_form_for @user, defaults: { input_html: { class: 'default_class' } } do |f| %>
  <%= f.input :username, input_html: { class: 'special' } %>
  <%= f.input :password, input_html: { maxlength: 20 } %>
  <%= f.input :remember_me, input_html: { value: '1' } %>
  <%= f.button :submit %>
<% end %>

Since Simple Form generates a wrapper div around your label and input by default, you can pass any html attribute to that wrapper as well using the :wrapper_html option, like so:

<%= simple_form_for @user do |f| %>
  <%= f.input :username, wrapper_html: { class: 'username' } %>
  <%= f.input :password, wrapper_html: { id: 'password' } %>
  <%= f.input :remember_me, wrapper_html: { class: 'options' } %>
  <%= f.button :submit %>
<% end %>

= f.button :submit, id: "whatever"

如果您查看 documentation,它接受一个可选字符串和一个选项散列(对于 class 或 id)。在内部,它只是调用 submit_tag 帮助器,这就是为什么它不同于 input 帮助器,后者在 input_html 哈希中传递任何 HTML 属性。