1
我正嘗試使用form_tag在搜索表單中設置一些動態下拉選擇菜單。我想是類似的功能在Railcasts #88Rails4動態選擇下拉列表
模型中發現的例子:
class Count < ActiveRecord::Base
belongs_to :host
end
class Host < ActiveRecord::Base
belongs_to :site
has_many :counts
end
class Site < ActiveRecord::Base
belongs_to :state
has_many :hosts
end
class State < ActiveRecord::Base
has_many :sites
end
查看:
<%= form_tag(counts_path, :method => "get", id: "search-form") do %>
<%= select_tag "state_id", options_from_collection_for_select(State.all.order(:name), :id, :name) %>
<%= select_tag "site_id", options_from_collection_for_select(Site.all.order(:name), :id, :name) %>
<% end %>
一個國家的has_many如果網站的has_many其中有許多計數主機。或者相反,Counts belongs_to主機whichs belongs_to屬於國家的網站
因此,我想從狀態下拉菜單中選擇一個狀態,然後根據它們通過主機關聯的狀態對網站進行「分組」。
我一直在與這個嵌套關聯掙扎,似乎無法弄清楚如何構建grouped_collection_select。
我知道我忽略了一些明顯的東西!可以肯定地使用一些指針...