Ruby on rails 国家/地区有许多频道选择复选框
我觉得这应该很简单,但我在工作中遇到了一些问题。我试过HABTM,但我认为它不是我需要的Ruby on rails 国家/地区有许多频道选择复选框,ruby-on-rails,Ruby On Rails,我觉得这应该很简单,但我在工作中遇到了一些问题。我试过HABTM,但我认为它不是我需要的 一个“国家”有许多“频道”,而一个“频道”属于一个“国家”。基本上,我想在频道表单上列出带有复选框的国家,并让它在国家id中保存一组国家 以下是观点: <%= f.label :country_id, "Countries" %><br /> <ul style="padding: 0; margin: 0;"> <% for country in Countr
一个“国家”有许多“频道”,而一个“频道”属于一个“国家”。基本上,我想在频道表单上列出带有复选框的国家,并让它在国家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>
-
“频道[国家/地区ID][]”%>
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
class Country
channel.rb
class Channel < ActiveRecord::Base
belongs_to :countries
attr_accessible :name, :logo, :country_id
end
class频道
我也将在卫星、统计数据、证词和视频方面做同样的事情
感谢您的帮助。谢谢
仅供参考,我是在Rails 2.3.8而不是Rails 3中这样做的。你说:
拯救世界上的一系列国家 国家识别号 在频道上指定country_id时,表示该频道属于一个国家。这听起来更像是你想要一个频道有很多国家,也许你想要的是M:M关系?无论哪种方式,您都无法将数组“保存”到country_id或模型上的任何字段……至少您无法完成您想要完成的任务
另外,
属于:国家
应该是属于:国家
如果您想在频道表的字符串字段中存储国家id列表,我会这样做:
(我不是100%肯定它能在Rails2.3中工作,但它应该工作,也许需要稍微调整一下)
在表单视图中:
<% Country.find(:all).each do |country| %>
<%= check_box_tag "channel[country_ids][#{country.id}]", country.id, false, :name => "channel[country_ids][]" %><%= label_tag "country[country_ids][#{country.id}]", country.description %>
<% end %>
“频道[国家/地区ID][]”%>
在模型中:
class Channel < ActiveRecord::Base
before_create :prepare_for_create
attr_accessible :country_ids
def prepare_for_create
self.country_ids = self.country_ids.join(",")
end
end
class频道
“在国家/地区id中保存一组国家/地区”…嗯?