Мне нужна помощь в реализации вложенной формы для 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 %>