Rails Ruby Rails 表格中的多个分隔线 Selector_tag

Rails multiple dividers in a Ruby on Rails form Selector_tag

我在 rails 表单的标准 ruby 中有一个选择器。选择器用于国家和大陆的分隔符。但我不想对其中国家/地区的值进行硬编码。我正在从控制器返回多个国家/地区列表(每个大陆一个)。我想把它放在那里而不是硬编码数组。

我有:

HTML

  <%  grouped_options = [['North America',['USA','Mexico']],['Europe',['Denmark','Germany','France']]] %>

  <%= f.select :country_id, grouped_options_for_select(grouped_options), {:include_blank => "Please select a country"}, {:required => true, :class => "white_background"} %>

我想要的:

   <%  grouped_options = [['North America',@north_america_names],['Europe',@europe_names]] %>

编辑: 此选项不起作用,我不确定为什么?

控制器

   @north_america_names = Country.select(:name, :id).where("continent = 'North America').distinct
   @europe_names = Country.select(:name, :id).where("continent = 'Europe').distinct

控制器工作得很好,它 returns 一个名称和 ID 列表。

编辑 2:

这是我以前的(没有分隔线),效果也很好。

HTML

 <%= f.collection_select(:country, @country_list, :id, :name, {:include_blank => "Please select a country"}, {:required => true, :class => "white_background"}) %>

控制器

 @country_list = Country.select(:name, :id).all

您需要显示来自 @north_america_names@europe_names

的迭代 data
<%  grouped_options = [['North America',@north_america_names.collect {|v| [ v.name, v.id ] }],['Europe',@europe_names.collect {|v| [ v.name, v.id ] }]] %>