【问题标题】:How to specify the id of a select element generated with Rails 5 association helper如何指定使用 Rails 5 关联助手生成的选择元素的 id
【发布时间】:2018-01-25 23:43:33
【问题描述】:

我需要为元素指定一个不同的 id,但我得到的是 html select 元素的自动生成的 id:

我的代码:

<%= f.association(
  :manager,
  label: 'Manager',
  id: 'not_doing_anything',
  collection: [@company.manager].compact
) %>

如何指定不同的 id?

【问题讨论】:

    标签: html select associations ruby-on-rails-5


    【解决方案1】:

    当您尝试使用下面的 html{} 时它是否有效?

             <%= f.association(
              :manager,
              label: 'Manager',
              :html => { id: "some_new_id" },
              collection: [@company.manager].compact
            ) %>
    

    【讨论】:

    • 运气不好,甚至 :select_html。我很难找到有关此的明确文档...
    【解决方案2】:

    我想通了:

    <%= f.association(
      :manager,
      label: 'Manager',
      id: 'not_doing_anything',
      collection: [@company.manager].compact,
      input_html: { :id => 'my_own_id'}
    ) %>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-22
      • 1970-01-01
      • 1970-01-01
      • 2016-05-05
      • 2018-01-13
      • 1970-01-01
      • 1970-01-01
      • 2014-12-28
      相关资源
      最近更新 更多