我覺得這應該是簡單的,但我有問題得到它的工作。我試過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