gpt4 book ai didi

ruby-on-rails-4 - ActiveAdmin 嵌套表单多选

转载 作者:行者123 更新时间:2023-12-04 07:12:11 24 4
gpt4 key购买 nike

我正在尝试通过产品表单更新 product_suppliers。该表单显示供应商表中的所有供应商,但不更新连接表。不确定错误在哪里。索引和显示显示正确的详细信息,但编辑不会更新连接表。开始在这个上绕来绕去。

更新:将表格更改为下面的内容让我很接近。但仍然没有更新连接表。但是,如果我手动将行添加到连接表,删除会按预期工作。它们显示并且可以被删除。保存将新的 product_id 添加到行中,而不是关联的 supply_company_id 值。我认为这是一个属性问题,但我看不到它。

应用程序/模型/product.rb

 class Product < ActiveRecord::Base
### shortned for 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

应用程序/模型/supply_company.rb
 class SupplyCompany < ActiveRecord::Base
has_many :products, :through => :product_suppliers
has_many :product_suppliers, :foreign_key => 'supply_company_id'
end

应用程序/模型/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 do

# 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}) do |f|
f.inputs "Product Details" do
f.input :id
f.input :product_name
f.input :product_description

#######################################################################
# Problem Lies with this piece of code Not saving the supply_company_id
# when adding a new row or updating the old rows. Delete works fine.
# cant see the error in models or permited_params.......
#######################################################################

f.inputs "Suppliers" do
f.has_many :product_suppliers do |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" do
f.has_many :product_images do |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 do |t|
t.integer "product_id"
t.integer "supply_company_id"
t.datetime "created_at"
t.datetime "updated_at"
end

更新:
将表格更改为下面的内容让我很接近。但仍然没有更新连接表。但是,如果我手动将行添加到连接表,删除会按预期工作。它们显示并且可以被删除。
保存将新的 product_id 添加到行中,而不是关联的 supply_company_id 值 .我认为这是一个属性问题,但我看不到它。
   f.inputs "Suppliers" do
f.has_many :product_suppliers do |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

最佳答案

原来它是代码中的 multipart::true 。一旦我从下面的代码中删除它,一切都按预期工作。

       form(:html => {:multipart => true}) do |f|
f.inputs "Product Details" do
f.input :id
f.input :product_name
f.input :product_description
f.has_many :product_supply_companies do |ff|
###############################################
#REMOVED multipart: :true from the 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" do
f.has_many :product_images do |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

关于ruby-on-rails-4 - ActiveAdmin 嵌套表单多选,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30565210/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com