0
 <%= f.label "SELECT MODEL LOCATION *" %><br />
      <div class="list_number">1</div><%= f.collection_select :location_id, Location.all, :id, :name, prompt: true, :required => true,  :class => 'chosen-select' %>

      <%= f.label "SELECT YOUR MODEL *" %><br />
      <div class="list_number">2</div>
      <%= f.grouped_collection_select :performer_id, Location.order(:name), :performers, :name, :id, :first_name, include_blank: true, :required => true,  class: 'chosen-select' %>

      <%= f.label "SELECT YOUR CATEGORY *" %><br />
      <div class="list_number">3</div>
    <%= f.select :clip_category_id, grouped_options_for_select(Performer.order(:first_name).map{ |group| [group.first_name, group.clip_category_performers.map{ |c| [c.clip_category.name, c.clip_category.id, {'data-amount'=>c.amount}] } ] }), include_blank: true, required: true, class: 'chosen-select' %>

I have used this collection select and as you see I have used chosen select Jquery plugin using chosen-rails gem. I am not able to make collection-select work. I am not able to set class for the collection select even though I have given here in the code when I inspect using inspect element in browser it won't show any class. What am I doing wrong?

4

1 回答 1

1

您可能需要更明确地拆分 rails select 选项和 html 选项。我还发现,有时您可以根据需要链接所有选项,有时您需要将选项拆分为两个散列。有关详细信息,请参阅文档:

http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/grouped_collection_select

现在尝试更多类似的东西(注意选择选项和 html 选项的井号括号):

<%= f.grouped_collection_select :performer_id, Location.order(:name), :performers, :name, :id, :first_name, { include_blank: true, :required => true },  { class: 'chosen-select' } %>

此外,您可以从不再积极开发的过时的 Chosen 插件切换到 Select2。它基于 Chosen,并提供了更多的功能和改进。http://ivaynberg.github.io/select2/index.html

不过,Chosen 很可能不对您的问题负责,因此您应该先处理类问题后再进行切换。

于 2013-09-20T11:39:17.710 回答