【发布时间】:2011-09-30 14:30:18
【问题描述】:
我需要帮助来实现 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 %>
【问题讨论】:
-
好吧,我不确定如何实现嵌套类别。正在寻求帮助。
标签: ruby-on-rails-3 has-and-belongs-to-many