Rails 3 has_and_belongs_to_many 和accepts_nested_attributes_for 实现
我需要帮助实现 has_and_belongs_to_many 的嵌套表单
我有以下内容:
模型
class Country < ActiveRecord::Base
has_and_belongs_to_many :categories
accepts_nested_attributes_for :categories, :allow_destroy => true
end
class Category < ActiveRecord::Base
has_and_belongs_to_many :countries
end
迁移视图
class CreateCountries < ActiveRecord::Migration
def self.up
create_table :countries do |t|
t.string :name
t.string :code
t.boolean :active
t.timestamps
end
end
def self.down
drop_table :countries
end
end
class CreateCategories < ActiveRecord::Migration
def self.up
create_table :categories do |t|
t.sting :name
t.string :description
t.boolean :active
t.timestamps
end
end
def self.down
drop_table :categories
end
end
class CreateCategoriesCountries < ActiveRecord::Migration
def self.up
create_table :categories_countries, :id => false do |t|
t.references :category
t.references :country
end
add_index(:categories_countries, [:category_id, :country_id], :unique => true)
end
def self.down
drop_table :categories_countries
end
end
我想要以复选框形式嵌套类别的国家/地区的
<%= form_for @country do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<p>
<%= f.label :code %><br />
<%= f.text_field :code %>
</p>
<p>
<%= f.label :active %><br />
<%= f.check_box :active %>
</p>
<p><%= f.submit %></p>
<% end %>
I need help with implementing the nested form for a has_and_belongs_to_many
I have the following:
Models
class Country < ActiveRecord::Base
has_and_belongs_to_many :categories
accepts_nested_attributes_for :categories, :allow_destroy => true
end
class Category < ActiveRecord::Base
has_and_belongs_to_many :countries
end
Migrations
class CreateCountries < ActiveRecord::Migration
def self.up
create_table :countries do |t|
t.string :name
t.string :code
t.boolean :active
t.timestamps
end
end
def self.down
drop_table :countries
end
end
class CreateCategories < ActiveRecord::Migration
def self.up
create_table :categories do |t|
t.sting :name
t.string :description
t.boolean :active
t.timestamps
end
end
def self.down
drop_table :categories
end
end
class CreateCategoriesCountries < ActiveRecord::Migration
def self.up
create_table :categories_countries, :id => false do |t|
t.references :category
t.references :country
end
add_index(:categories_countries, [:category_id, :country_id], :unique => true)
end
def self.down
drop_table :categories_countries
end
end
View for Country where I want the nested categories in the form of checkboxes
<%= form_for @country do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<p>
<%= f.label :code %><br />
<%= f.text_field :code %>
</p>
<p>
<%= f.label :active %><br />
<%= f.check_box :active %>
</p>
<p><%= f.submit %></p>
<% end %>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
来源: 使用 ` 处理复选框表单: has_many :通过`记录关联 => http://millarian.com/programming/ruby -on-rails/quick-tip-has_many-through-checkboxes/
为什么使用 :allow_destroy =>真的 ?
source: Handle check box forms with an `:has_many :through` Record Association => http://millarian.com/programming/ruby-on-rails/quick-tip-has_many-through-checkboxes/
Why do you use :allow_destroy => true ?