rails 4: 添加一个 class 到一个 select 标签

rails 4: Adding a class to a select tag

正在尝试将 class 添加到 select 标签

<%= f.select(:ptype, options_for_select(TourParticipation::PTYPES, selected: "traveler", disabled: "Role:") , { :class => "widens" } ) %>

结果

<select name="tour_participation[ptype]" id="tour_participation_ptype"></select>

为什么 class 没有应用到我的 select 标签?

你忘了在html_options之前是options哈希。试试这个:

<%= f.select(:ptype, options_for_select(TourParticipation::PTYPES, selected: "traveler", disabled: "Role:") , {}, { :class => "widens" } ) %>

见定义: http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-select