如何允许模型的所有属性
how to allow all of the attributes of a model when
我正在将 Rails 3.2 应用程序迁移到 strong_parameters,但没有太多经验。
我有一个名为 Item 的模型,它具有 has_many 属性。在我们的 item#update 中,我希望能够执行以下操作:
# Model
class Item < ActiveRecord::Base
include ActiveModel::ForbiddenAttributesProtection
has_many :assets, :as => :assetable, :dependent => :destroy
...
#in items_controller.rb
def update
@item=Item.find(params[:id])
if @item.update_attributes(params[:item])
...
private
def item_params
params.require(:item).permit(:assets_attributes).permit!
end
如何指定 item_params 以允许通过此更新语句创建资产?
编辑 1
所以如果我通过以下方式提取属性列表:
a=Asset.first
a.attributes
我得到:
{"id"=>4424,
"name"=>nil,
"created_at"=>Fri, 24 Jan 2014 15:49:17 PST -08:00,
"updated_at"=>Fri, 24 Jan 2014 15:49:17 PST -08:00,
"asset_file_name"=>"br-3.jpg",
"asset_content_type"=>"image/jpeg",
"asset_file_size"=>198085,
"asset_updated_at"=>Fri, 24 Jan 2014 15:49:16 PST -08:00,
"menu_item_id"=>nil,
"assetable_id"=>1,
"assetable_type"=>"LocationProfileAlbum",
"global_id"=>9394,
"description"=>nil,
"associated_global_id"=>9393,
"user_id"=>nil,
"position"=>0.0,
"hash_val"=>nil,
"is_instore"=>false,
"location_id"=>nil,
"filepicker_url"=>nil}
如果我再把它放在:
def item_params
params.require(:item).permit(
:assets_attributes[
:id, :name, :created_at, :updated_at , :asset_file_name, :asset_content_type, :asset_file_size, :asset_updated_at, :menu_item_id, :assetable_id, :assetable_type, :global_id, :description, :associated_global_id, :user_id, :position, :hash_val, :is_instore, :location_id, :filepicker_url
]
)
然后添加一个文件,我得到错误:
ArgumentError (wrong number of arguments (20 for 1..2)):
app/controllers/items_controller.rb:218:in `[]'
app/controllers/items_controller.rb:218:in `item_params'
首先,您需要指定允许嵌套资产的哪些属性,例如:
def item_params
params.require(:item).permit(assets_attributes: [:col1, :col2, :col3])
end
然后,请确保在更新时使用此私有方法 @item
:
@item.update_attributes item_params
ETA(基于您的 edit1):回形针 POST 实际文件,而不是其属性,因此您会:
params.require(:item).permit(assets_attributes: [:asset])
为了将来参考,您可以在日志中找到传递给操作的参数。它看起来像:
Parameters: {"utf8"=>"✓", "item"=>{"assets_attributes"=>[{"asset"=>#<ActionDispatch::Http::UploadedFile…>}]}
日志还将包括任何参数已被强参数排除的通知。这对于确定需要允许哪些参数非常有帮助。
我也强烈反对将所有参数添加到允许列表中。强参数是出于严重的安全问题,即攻击者能够编辑他们不应该访问的字段。基本上,请记住 任何人 有权访问该网页将能够 post 允许列表中任何参数的任何值。
我正在将 Rails 3.2 应用程序迁移到 strong_parameters,但没有太多经验。
我有一个名为 Item 的模型,它具有 has_many 属性。在我们的 item#update 中,我希望能够执行以下操作:
# Model
class Item < ActiveRecord::Base
include ActiveModel::ForbiddenAttributesProtection
has_many :assets, :as => :assetable, :dependent => :destroy
...
#in items_controller.rb
def update
@item=Item.find(params[:id])
if @item.update_attributes(params[:item])
...
private
def item_params
params.require(:item).permit(:assets_attributes).permit!
end
如何指定 item_params 以允许通过此更新语句创建资产?
编辑 1
所以如果我通过以下方式提取属性列表:
a=Asset.first
a.attributes
我得到:
{"id"=>4424,
"name"=>nil,
"created_at"=>Fri, 24 Jan 2014 15:49:17 PST -08:00,
"updated_at"=>Fri, 24 Jan 2014 15:49:17 PST -08:00,
"asset_file_name"=>"br-3.jpg",
"asset_content_type"=>"image/jpeg",
"asset_file_size"=>198085,
"asset_updated_at"=>Fri, 24 Jan 2014 15:49:16 PST -08:00,
"menu_item_id"=>nil,
"assetable_id"=>1,
"assetable_type"=>"LocationProfileAlbum",
"global_id"=>9394,
"description"=>nil,
"associated_global_id"=>9393,
"user_id"=>nil,
"position"=>0.0,
"hash_val"=>nil,
"is_instore"=>false,
"location_id"=>nil,
"filepicker_url"=>nil}
如果我再把它放在:
def item_params
params.require(:item).permit(
:assets_attributes[
:id, :name, :created_at, :updated_at , :asset_file_name, :asset_content_type, :asset_file_size, :asset_updated_at, :menu_item_id, :assetable_id, :assetable_type, :global_id, :description, :associated_global_id, :user_id, :position, :hash_val, :is_instore, :location_id, :filepicker_url
]
)
然后添加一个文件,我得到错误:
ArgumentError (wrong number of arguments (20 for 1..2)):
app/controllers/items_controller.rb:218:in `[]'
app/controllers/items_controller.rb:218:in `item_params'
首先,您需要指定允许嵌套资产的哪些属性,例如:
def item_params
params.require(:item).permit(assets_attributes: [:col1, :col2, :col3])
end
然后,请确保在更新时使用此私有方法 @item
:
@item.update_attributes item_params
ETA(基于您的 edit1):回形针 POST 实际文件,而不是其属性,因此您会:
params.require(:item).permit(assets_attributes: [:asset])
为了将来参考,您可以在日志中找到传递给操作的参数。它看起来像:
Parameters: {"utf8"=>"✓", "item"=>{"assets_attributes"=>[{"asset"=>#<ActionDispatch::Http::UploadedFile…>}]}
日志还将包括任何参数已被强参数排除的通知。这对于确定需要允许哪些参数非常有帮助。
我也强烈反对将所有参数添加到允许列表中。强参数是出于严重的安全问题,即攻击者能够编辑他们不应该访问的字段。基本上,请记住 任何人 有权访问该网页将能够 post 允许列表中任何参数的任何值。