<%semantic_form_for(@ rouss,{:url => url,:html => {....}})do | f | %GT;
....
<%= f.input:country,:label => 'Country:',: include_blank => true%>
....
和国家/地区选择呈现为..
<li class="select required" id="business_country_input">
<label for="business_country_id">Country:</label>
<select id="business_country_id" name="business[country_id]">
<option value=""></option>
<option value="221">Åland Islands</option>
<option value="32">Algeria</option>
<option value="9">American Samoa</option>
</select>
</li>
那么,我如何为选项标签添加额外的参数,例如我需要像
这样的东西 .....
<option value="221" country-code="AA">Aland Islands</option>
<option value="32" country-code="BB">Algeria</option>
<option value="9" country-code="CC">American Samoa</option>
.....
基本上像
<select id="country">
<% @countries.each do |c| %>
<option value="<%= c.id %>" country-code="<%= c.code %>"><%= c.name %></option>
<% end %>
</select>
如何用formtastic完成?。
答案 0 :(得分:4)
首先定义一个帮助程序来构建自定义国家/地区选项:
def custom_country_options
countries = ActionView::Helpers::FormOptionsHelper::COUNTRIES.map do |c|
[c.downcase, c, {:country-code => c[0..1]}]
#=> ["algeria", "Algeria", "Al"]
end
return options_for_select(countries)
end
然后在您的表单中,您应该能够:
<%= f.input :country, :as => :select, :collection => custom_country_options %>
注意:f.input :country
有可能始终创建默认国家/地区下拉菜单,您可能必须执行f.input :custom_country
或类似操作。