ruby on rails 4 - ActiveAdmin Nested Form multiple select -
i'm trying product_suppliers update via product form. form displays suppliers in supplier table doesn't update join table. not sure error lies. index , show show correct details edit not updating join table. starting go around , around in circles on one.
update: changing form below has got me close. still not updating join table. delete works expected if manually add rows join table. display , can deleted. saving adds new product_id row not associated supply_company_id value. figure attribute issue cant see it.
app/models/product.rb
class product < activerecord::base ### shortned clarity has_many :product_suppliers, :foreign_key => 'product_id' has_many :supply_companies, :through => :product_suppliers accepts_nested_attributes_for :product_suppliers, :allow_destroy => true end
app/models/supply_company.rb
class supplycompany < activerecord::base has_many :products, :through => :product_suppliers has_many :product_suppliers, :foreign_key => 'supply_company_id' end
app/models/product_supplier.rb
class productsupplier < activerecord::base belongs_to :product belongs_to :supply_company accepts_nested_attributes_for :product accepts_nested_attributes_for :supply_company end
/app/admin/product.rb
activeadmin.register product # see permitted parameters documentation: # https://github.com/activeadmin/activeadmin/blob/master/docs/2-resource-customization.md#setting-up-strong-parameters # permit_params :id, :product_name, :product_description, :product_type_id, :product_category_id, :product_colour_id, :product_size_id, product_images_attributes: [:id, :product_id, :product_image, :_destroy], product_types_attributes: [:id, :product_type], product_catergories_attributes: [:id, :product_category], product_colour_attributes: [:id, :product_colour], product_size_attributes: [:id, :product_size], product_suppliers_attributes: [:id, :product_id, :supply_company_id, :_destroy], supply_companies_attributes: [:id, :company_name] form(:html => {:multipart => true}) |f| f.inputs "product details" f.input :id f.input :product_name f.input :product_description ####################################################################### # problem lies piece of code not saving supply_company_id # when adding new row or updating old rows. delete works fine. # cant see error in models or permited_params....... ####################################################################### f.inputs "suppliers" f.has_many :product_suppliers |ff| ff.input :supply_company_id, as: :select, multiple: true, collection: supplycompany.all.map {|u| [u.company_name.to_s, u.id]} ff.input :_destroy, :as=>:boolean, :required => false, :label => 'remove supplier' end end ######################################################## f.input :product_type_id, :as => :select, :collection => producttype.all.map {|u| [u.product_type.to_s, u.id]} f.input :product_category_id, :as => :select, :collection => productcategory.all.map {|u| [u.product_category.to_s, u.id]} f.input :product_colour_id, :as => :select, :collection => productcolour.all.map {|u| [u.product_colour.to_s, u.id]} f.input :product_size_id, :as => :select, :collection => productsize.all.map {|u| [u.product_size.to_s, u.id]} end f.inputs "product images" f.has_many :product_images |p| p.input :product_image, :as => :file, :label => "image",:hint => image_tag(p.object.product_image.url(:thumb)) p.input :_destroy, :as=>:boolean, :required => false, :label => 'remove image' end end f.actions end
product_suppliers_schema
create_table "product_suppliers", force: true |t| t.integer "product_id" t.integer "supply_company_id" t.datetime "created_at" t.datetime "updated_at" end
update: changing form below has got me close. still not updating join table. delete works expected if manually add rows join table. display , can deleted. saving adds new product_id row not associated supply_company_id value. figure attribute issue cant see it.
f.inputs "suppliers" f.has_many :product_suppliers |ff| ff.input :supply_company_id, as: :select, multiple: true, collection: supplycompany.all.map {|u| [u.company_name.to_s, u.id]} ff.input :_destroy, :as=>:boolean, :required => false, :label => 'remove supplier' end end
turns out multipart: :true in code. once removed below code worked expected.
form(:html => {:multipart => true}) |f| f.inputs "product details" f.input :id f.input :product_name f.input :product_description f.has_many :product_supply_companies |ff| ############################################### #removed multipart: :true line below ############################################### ff.input :supply_company_id, as: :select, collection: supplycompany.all.map {|u| [u.company_name.to_s, u.id]} ff.input :_destroy, :as=>:boolean, :required => false, :label => 'remove supplier' end f.input :product_type_id, :as => :select, :collection => producttype.all.map {|u| [u.product_type.to_s, u.id]} f.input :product_category_id, :as => :select, :collection => productcategory.all.map {|u| [u.product_category.to_s, u.id]} f.input :product_colour_id, :as => :select, :collection => productcolour.all.map {|u| [u.product_colour.to_s, u.id]} f.input :product_size_id, :as => :select, :collection => productsize.all.map {|u| [u.product_size.to_s, u.id]} end f.inputs "product images" f.has_many :product_images |p| p.input :product_image, :as => :file, :label => "image",:hint => image_tag(p.object.product_image.url(:thumb)) p.input :_destroy, :as=>:boolean, :required => false, :label => 'remove image' end end f.actions end
Comments
Post a Comment