Ruby on rails Rails activeadmin将数据保存在关联表中
我有一张产品表,产品可以是内部的,也可以是外部的,或者两者都有。所以我创建了另一个表来保存产品位置。现在,当管理员添加产品时,我提供了选择产品所在位置的选项,但在发布时,代码表示由于验证,该字段不能为空。我不确定我遗漏了什么或者方法是错误的 产品型号:Ruby on rails Rails activeadmin将数据保存在关联表中,ruby-on-rails,activeadmin,has-many,Ruby On Rails,Activeadmin,Has Many,我有一张产品表,产品可以是内部的,也可以是外部的,或者两者都有。所以我创建了另一个表来保存产品位置。现在,当管理员添加产品时,我提供了选择产品所在位置的选项,但在发布时,代码表示由于验证,该字段不能为空。我不确定我遗漏了什么或者方法是错误的 产品型号: class Product < ApplicationRecord validates :name, presence: true has_many :product_locations accepts_nest
class Product < ApplicationRecord
validates :name, presence: true
has_many :product_locations
accepts_nested_attributes_for :product_locations
end
class ProductLocation < ApplicationRecord
enum locations: [:exterior, :interior]
validates :location, presence: true
validates :product_id, presence: true
belongs_to :product
end
ActiveAdmin.register Product do
permit_params :name, product_locations_attributes: {}
actions :all, except: [:show, :destroy]
filter :name
index do
column 'Product Name', :name
actions
end
form do |f|
f.semantic_errors *f.object.errors.keys
f.inputs "Products" do
f.input :name
end
f.has_many :product_locations do |location|
location.inputs "Locations" do
location.input :location, as: :select, multiple: true, collection: ProductLocation.locations.keys
end
end
f.actions
end
controller do
def scoped_collection
Product.where(user_id: nil)
end
end
end
我得到了一个多选择的位置,其中有“内部”和“外部”的选择,但它说,该字段不能为空,当我选择的位置和提交表格
“保存”单击“获取”时出错:
位置不能为空
发布的参数包括:
Parameters: {"utf8"=>"✓", "product"=>{"name"=>"Test Product", "product_locations_attributes"=>{"0"=>{"location"=>["0", "1"]}}}, "commit"=>"Create Product"}
首先,许可属性应为
产品位置属性:[:id,:location]
然后,以你的形式
location.input :location, as: :select, multiple: true, collection: ProductLocation.locations.keys
由于ProductLocation.locations
是一个数组,因此array.keys
是一个无效的方法。
所以,直接使用
location.input :location, as: :select, multiple: true, collection: ProductLocation.locations.map { |n| [n,n] }
要存储多个值的数组,请将字段序列化为数组,
class ProductLocation < ApplicationRecord
enum locations: [:exterior, :interior]
serialize :location, Array
validates :location, presence: true
validates :product_id, presence: true
belongs_to :product
end
class ProductLocation
注意:为了获得序列化工作,您需要将位置的数据类型
作为文本
。如果不是text
则运行迁移以更改为text
数据类型
文本字段的原因:Rails将在数据库中存储时将所有这些对象转换为纯文本首先,应
产品位置属性:[:id,:location]
然后,以你的形式
location.input :location, as: :select, multiple: true, collection: ProductLocation.locations.keys
由于ProductLocation.locations
是一个数组,因此array.keys
是一个无效的方法。
所以,直接使用
location.input :location, as: :select, multiple: true, collection: ProductLocation.locations.map { |n| [n,n] }
要存储多个值的数组,请将字段序列化为数组,
class ProductLocation < ApplicationRecord
enum locations: [:exterior, :interior]
serialize :location, Array
validates :location, presence: true
validates :product_id, presence: true
belongs_to :product
end
class ProductLocation
注意:为了获得序列化工作,您需要将位置的数据类型
作为文本
。如果不是text
则运行迁移以更改为text
数据类型
文本字段的原因:Rails将所有这些对象存储在数据库中时转换为纯文本确切的错误是什么?更新问题中的内容。@Pavan请检查我想您应该将
permit\u params:name,product\u locations\u attributes:{}
更改为permit\u params:name,product\u locations\u attributes:[:location]
我想这一行,location.input:location,as::select,multiple:true,集合:ProductLocation.locations.keys
应该是location.input:location,as::select,multiple:true,集合:ProductLocation.locations
,因为对于array@Pavan,但它会说“Unpermitted parameter::location”,确切的错误是什么?更新问题中的内容。@Pavan请检查我想您应该将permit\u params:name,product\u locations\u attributes:{}
更改为permit\u params:name,product\u locations\u attributes:[:location]
我想这一行,location.input:location,as::select,multiple:true,集合:ProductLocation.locations.keys
应该是location.input:location,as::select,multiple:true,集合:ProductLocation.locations
,因为对于array@Pavan,但它会说“Unpermitted parameter::location”@DeepanshuGoyal,您应该将字段类型设置为serialize
以存储多个值。我得到了这个错误:“不能转储节
:应该是一个数组,但它是一个整数。--0“好的,明白了,将collection更改为,ProductLocation.locations.map{n |[n,n]}
,但它仍然给我错误信息无法转储节:应为数组,但为整数。-0“让我们@DeepanshuGoyal,您应该将字段类型设置为序列化
,以存储多个值。我收到此错误:“无法转储节
:应为数组,但为整数。-0“好的,明白了,将collection更改为,ProductLocation.locations.map{|n |[n,n]}
,但它仍然给我错误“无法转储节:应该是一个数组,但却是一个整数。--0“让我们看看。