Hi
I have a problem with has_many :trough association. I have 2 tables,
Product and Category, and when I create new Product, I should make
checkboxes to choose in what categories the product . I''ve made model
category_product where I store the association from the Category and
Product tables. Here is some of the code.
class Category < ActiveRecord::Base
has_many :category_products, dependent: :destroy
has_many :products, :through => :category_products
accepts_nested_attributes_for :category_products
class CategoryProduct < ActiveRecord::Base
belongs_to :category
belongs_to :product
accepts_nested_attributes_for :category
class Product < ActiveRecord::Base
has_many :category_products, dependent: :destroy
has_many :categories, :through => :category_products
accepts_nested_attributes_for :category_products
class ProductsController < ApplicationController
def new
@product = Product.new
end
def create
@product = Product.new(product_params)
if @product.save
items_array = category_params
items_array.each do |key, value|
if value != ""
@category_product = CategoryProduct.new(:category => value,
:product => @product.id)
@category_product.save
end
#category_products.create!(:category => arrayitem.id, :product =>
@product.id)
end
#@categories = Category.find(category_params)
# @categories.each do |category|
#category_products.create!(:category => category.id, :product =>
@product.id)
#end
flash[:source] = ''Successful''
redirect_to @product
else
render ''new''
end
end
def category_params
params.require(:product).permit({:category_ids => [:id]})
end
def product_params
params.require(:product).permit(:name, :price, :sale_status, :quantity,
:release_date, {:category_ids => [:id]})
end
class CategoryProductsController < ApplicationController
def create
@category_product = CategoryProduct.new(params[:product_ids,
:category_ids])
if @category_product.save
#sign_in @user
#flash[:success] = "Welcome to the Sample App!"
#redirect_to @user
else
#render ''new''
end
end
end
products/new.html.erb
<%= form_for(@product) do |f| %>
<%=f.collection_check_boxes :category_ids, Category.all, :id, :name
#
# f.association :categories,
# as: :check_boxes,
# label_method: :name,
# value_method: :id,
# label: ''Select Category''
%>
This is basically all the code about this problem. If you can send me some
suggestions how to solve it, or some tutorials about this kind of
situations I would be grateful.
Thanks.
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To view this discussion on the web visit
https://groups.google.com/d/msgid/rubyonrails-talk/3eab64e7-d0a4-4ef2-9827-434c2f7a3030%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.