我觉得这应该是简单的,但我有问题得到它的工作。我试过HABTM,但我不认为这是我需要的。国家has_many渠道选择复选框
'Country'has_many'Channels'和'Channel'belongs_to'Country'。基本上我想在渠道表格中列出带有复选框的国家,并在country_id中保存一系列国家。
这里的观点:
<%= f.label :country_id, "Countries" %><br />
<ul style="padding: 0; margin: 0;">
<% for country in Country.find(:all) %>
<li style="list-style: none;">
<%= check_box_tag "channel[country_ids][]", :name => "channel[country_ids][]" %>
<%= label_tag country.id, country.name %>
</li>
<% end %>
</ul>
country.rb
class Country < ActiveRecord::Base
has_many :channel
has_many :satellites
has_many :statistics
has_many :testimonies
has_many :videos
attr_accessible :name, :coords
def hash
name.gsub(" ", "_").downcase
end
end
channel.rb
class Channel < ActiveRecord::Base
belongs_to :countries
attr_accessible :name, :logo, :country_id
end
我会做同样的事情卫星,统计,证词和视频。
任何帮助表示赞赏。谢谢!
仅供参考,我用Rails 2.3.8和Rails的不这样做3.
“保存在COUNTRY_ID国家的数组” ......嗯? – tybro0103 2011-02-04 14:57:16