在rails中下拉列表

时间:2010-11-02 13:53:12

标签: ruby-on-rails ruby

    <% form.inputs do %>
      <% @account.account_preference.editorial_attributes.each do |key, value| %>
        <%= account_pref.input "editorial_#{key}".to_sym, :as => :radio, :collection => options_for(Editorial, key.to_sym), :wrapper_html => { :class => "compact" }, :label => key.titleize  %>
      <% end %>
    <% end %>

如何使用单选按钮将此下拉列表更改为常规下拉列表?

1 个答案:

答案 0 :(得分:0)

你可以这样做:

account_pref_options = []
@account.account_preference.editorial_attributes.each do |k,v|
  account_pref_options << [k.titleize, "editorial_#{key}"]
end

然后使用select标签助手。如果在form_for model do |f|标记内,请执行

<%= f.select :field_name, account_pref_options_options %>

如果有form_tag标记,请执行

<%= select_tag(:field_name, options_for_select(account_pref_options)) %>

查看EdgeGuides以获取更多信息以及RoR API。

http://edgeguides.rubyonrails.org/form_helpers.html#the-select-and-option-tags