Rails 3 has_and_belongs_to_many 和accepts_nested_attributes_for 实现

发布于 2024-11-18 20:09:16 字数 1745 浏览 8 评论 0原文

我需要帮助实现 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦罢 2024-11-25 20:09:16
<%= form_for @country do |f| %>
  <%= f.error_messages %>
  ...
  <% Categories.all.each do |category| %>
  <p>
    <%= f.label category.name %>
    // don't use f here
    <%= check_box :categories_ids,
                  category.id,
                  @country.categories.include?(category),
                  :name=>'country[categories_ids][]' %>
  </p>
  <% end %>
  <p><%= f.submit %></p>
<% end %> 

来源: 使用 ` 处理复选框表单: has_many :通过`记录关联 => http://millarian.com/programming/ruby -on-rails/quick-tip-has_many-through-checkboxes/

为什么使用 :allow_destroy =>真的 ?

<%= form_for @country do |f| %>
  <%= f.error_messages %>
  ...
  <% Categories.all.each do |category| %>
  <p>
    <%= f.label category.name %>
    // don't use f here
    <%= check_box :categories_ids,
                  category.id,
                  @country.categories.include?(category),
                  :name=>'country[categories_ids][]' %>
  </p>
  <% end %>
  <p><%= f.submit %></p>
<% end %> 

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 ?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文