samedi 7 mai 2016

How to add class to Rails select form helper when using as a block

The documentation for Rails select form helper states (see documentation):

select(object, method, choices = nil, options = {}, html_options = {}, &block)

Which allows adding a class simple, like so:

<%= f.select :some_attr, MYOPTIONS, {}, {class: 'my-class'} %>

My question is, how do I add a class to it when using it as a block? Rails documentation states:

select(report, "campaign_ids") do
  available_campaigns.each do |c|
    content_tag(:option, c.name, value: c.id, data: { tags: c.tags.to_json })
  end
end

It doesn't work when I use it like so:

<%= f.select :some_attr, {}, {class: 'my-class'} do %>
  <% MYOPTIONS.each do |MYOPTION| do %>
    <%= content_tag :option, MYOPTION.label, value: MYOPTION.value %>
  <% end %>
<% end %>

Nor does it work if I use:

f.select :some_attr, class: 'my-class' do

The class is not applied to the select tag in the HTML.

Aucun commentaire:

Enregistrer un commentaire