【问题标题】:Rails Simple form button idRails 简单表单按钮 ID
【发布时间】:2017-01-16 14:02:05
【问题描述】:

我无法让 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

正确的做法是什么?

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

      来自文档:

      如果您想将相同的选项传递给表单中的所有输入(例如,默认类),您可以使用 simple_form_for 中的 :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 %>
      

      由于 Simple Form 默认会围绕您的标签和输入生成一个包装器 div,因此您也可以使用 :wrapper_html 选项将任何 html 属性传递给该包装器,如下所示:

      <%= 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 %>
      

      【讨论】:

      • button 助手将选项哈希按原样传递给submit_tag 助手,它不适用于input_html 哈希。它将输出一个input_html 属性。
      • Rails 5 :| -
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-30
      • 2013-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多