Rails nested attributes - how to add a category attribute to a new product?

I use rails to create a new product and want to add a category to each product.

I have three tables: product, category and categories (which stores the relationship between products and categories). I am trying to use nested attributes to control the creation of categorization, but I don’t know how to update the controller and view / form so that new products also update the category table.

Here are my models:

class Product < ActiveRecord::Base
 belongs_to :users
 has_many :categorizations
 has_many :categories, :through => :categorizations
 has_attached_file :photo
 accepts_nested_attributes_for :categorizations, allow_destroy: true

 attr_accessible :description, :name, :price, :photo

 validates :user_id, presence: true

end


class Category < ActiveRecord::Base
 attr_accessible :description, :name, :parent_id
 acts_as_tree
 has_many :categorizations, dependent: :destroy
 has_many :products, :through => :categorizations

end


class Categorization < ActiveRecord::Base
  belongs_to :category
  belongs_to :product
  attr_accessible :category_id, :created_at, :position, :product_id

end

Here is my new product controller:

def new
    @product = Product.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @product }
    end
  end

And here is my presentation form:

<%= form_for @product, :html => { :multipart => true } do |f| %>
  <% if @product.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@product.errors.count, "error") %> prohibited this product from being saved:</h2>

      <ul>
      <% @product.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </div>
  <div class="field">
    <%= f.label :description %><br />
    <%= f.text_field :description %>
  </div>
  <div class="field">
    <%= f.label :price %><br />
    <%= f.number_field :price %>
  </div>
<div class="field">
<%= f.file_field :photo %>
</div>

  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

How do I update my controller to update both the product and the categorization tables when adding a new product? How to update the view file so that categories are displayed in the drop-down menu?

+5
4

, . , / . ( ). : / , :

 cat_1 [+]
 cat_2 [-]
 cat_3 [+]

Railcasts, , .

+4

, , ,

<%= select_tag("category_id[]", options_for_select(Category.find(:all).collect { |cat| [cat.category_name, cat.id] }, @product.category.collect { |cat| cat.id}))%>

-

@product = Product.create(params[:category])
@product.category = Category.find(params[:category_id]) if params[:category_id]

, . .

0

RailsCasts , , , , - .

0
source

Here is what I added to the product presentation file in the _form.html file - this created several checkboxes that I could use to select several categories for each product:

</div class="field">
<% Category.all.each do |category| %>
<%= check_box_tag "product[category_ids][]", category.id %>
<%= label_tag dom_id(category), category.name %><br>
<% end %>
0
source

All Articles