如何基于模型为grouped_options_for_select生成数组

时间:2011-05-11 12:03:33

标签: ruby-on-rails

产品belogs_to类别。 类别has_many产品

我需要一个包含optgroup标签的select标签。

基本上,我需要将我的HTML标记与伪代码类似:

<select name="product" class="products">
  <option value=""></option>
  <optgroup label="Cheese">
    <option value="1">cheese 1</option>
    <option value="2">cheese 2</option>
    <option value="3">cheese 3</option>
  </optgroup>
  <optgroup label="Juices">
    <option value="3">juice 1</option>
    <option value="4">juice 2</option>
    <option value="5">juice 3</option>
  </optgroup>
</select>

根据我的模型,我如何生成要放入 grouped_options_for_select 的数组?例如,生成 grouped_options 的最佳方法是什么?

<%= select_tag 'product', grouped_options_for_select(grouped_options), :include_blank => true , :id => nil, :class => 'product' %>

3 个答案:

答案 0 :(得分:13)

控制器:

@products = Product.all(:include => :category)
@grouped_options = @products.inject({}) do |options, product|
  (options[product.category.name] ||= []) << [product.name, product.id]
  options
end

查看:

<%= select_tag 'product', grouped_options_for_select(@grouped_options), :include_blank => true , :id => nil, :class => 'product' %>

答案 1 :(得分:1)

对于Rails 4

型号:

class Category < ActiveRecord::Base
  has_many :products
end

class Product < ActiveRecord::Base
  belongs_to :category
end

控制器:

@grouped_options = Category.all.map {
                           |category| [category.name, 
                             category.products.map {
                               |product| [product.name,product.id]}]}

查看:

<%= select_tag(:product, grouped_options_for_select(@grouped_options))%>

答案 2 :(得分:0)

我正在使用mongoid,我添加了'all'以便首先获得所有模型

([{"address":"178795010","client_id":null,"expire":"2016-09-26 16:56:32","fqdn_fwd":"1","fqdn_rev":"1","hostname":"vagrant-20009.ipam.bskyb","hwaddr":"0800272022E6","state":"0","subnet_id":"500","valid_lifetime":"4000"},{"address":"178795011","client_id":null,"expire":"2016-09-26 16:58:48","fqdn_fwd":"1","fqdn_rev":"1","hostname":"vagrant-18992.ipam.bskyb","hwaddr":"0800270FB593","state":"0","subnet_id":"500","valid_lifetime":"4000"},{"address":"178795012","client_id":null,"expire":"2016-09-26 17:13:55","fqdn_fwd":"1","fqdn_rev":"1","hostname":"dhcp-client1.ipam.bskyb","hwaddr":"0800275EA5B9","state":"0","subnet_id":"500","valid_lifetime":"4000"},{"address":"178795013","client_id":null,"expire":"2016-09-26 16:56:17","fqdn_fwd":"1","fqdn_rev":"1","hostname":"vagrant-10873.ipam.bskyb","hwaddr":"0800275FCA93","state":"0","subnet_id":"500","valid_lifetime":"4000"},{"address":"178795018","client_id":null,"expire":"2016-09-26 17:06:16","fqdn_fwd":"1","fqdn_rev":"1","hostname":"vagrant-24830.ipam.bskyb","hwaddr":"080027707E62","state":"0","subnet_id":"500","valid_lifetime":"4000"},{"address":"3232250120","client_id":null,"expire":"2016-09-26 16:17:15","fqdn_fwd":"1","fqdn_rev":"1","hostname":"bobtheblob7.ipam.bmarkskyb","hwaddr":"080027063AD9","state":"0","subnet_id":"12","valid_lifetime":"100"},{"address":"3232250122","client_id":null,"expire":"2016-09-26 16:17:22","fqdn_fwd":"1","fqdn_rev":"1","hostname":"bobtheblob8.ipam.bmarkskyb","hwaddr":"800027A5E27A","state":"0","subnet_id":"12","valid_lifetime":"100"}]);