如何修复此创建功能? [英] How can I fix this create function?
问题描述
我有一个函数可以将嵌套属性拆分为一个数组,以便检查它们的唯一性,然后保存它们.问题是,我收到一个 Can't mass-assign protected attributes: _destroy
错误,因为 nested_form gem
插入了一个 hidden_field 来突出显示哪些属性应该被删除.
这是 *Posts_Contoller.rb* 创建函数:
def 创建location_set = params[:post].delete(:locations_attributes) 除非 params[:post][:locations_attributes].blank?@post = current_blogger.blog_posts.new(params[:post])@post.locations = Location.find_or_initialize_location_set(location_set) 除非 location_set.nil?如果@post.saveredirect_to @post,注意:博客帖子已成功创建."别的渲染动作:新"结尾结尾
这是location.rb中的函数:
def self.find_or_initialize_location_set(location_set)#创建一个位置数组位置 = []位置=locations.delete_if { |元素|elem.flatten.empty?}location_set.each do |key, location|位置<
这是日志:
在 2012-12-03 17:31:54 +0000 开始为 127.0.0.1 POST "/blog/posts"由 Blogit::PostsController#create 处理为 HTML 参数:{"utf8"=>"✓", "authenticity_token"=>"r74iCzC4tJgVI6FiCEH7XzfiTmaqKihF5JSs7Ow3MSI=", "post"=>{"title"=>"This是一篇关于巴黎的文章", "body"=>"这是一篇关于巴黎的文章这是一篇关于巴黎的文章这是一篇关于巴黎的文章这是一篇关于巴黎的文章", "tag_list"=>"",locations_attributes"=>{0"=>{_destroy"=>false",名称"=>巴黎",经度"=>2.3522219",纬度"=>"48.856614"}, "1354555760002"=>{"_destroy"=>"false", "name"=>"洛杉矶", "longitude"=>"-118.5155901", "latitude"=>34.03563310000001"}}}, "_wysihtml5_mode"=>"1", "name"=>"losds", "legname"=>"洛杉矶", "longitude"=>"-118.5155901", "latitud"e"=>"34.03563310000001", "提交"=>"提交"}用户负载 (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1ActsAsTaggableOn::Tag Load (0.1ms) SELECT "tags".* FROM "tags" INNER JOIN "tags" ON "tags"."id" = "taggings"."tag_id" W这里标签".taggable_id"是空的,标签".taggable_type"=博客::发布"和(标签.上下文=标签"和标签.tagger_id是空的)位置负载 (0.1ms) SELECT "locations".* FROM "locations" WHERE "locations"."name" = 'Paris' LIMIT 1记录器位置是[#<Location id: 768, name: "Paris", latitude: #<BigDecimal:7f86aadf6230,'0.48856614E2',18(45)>,经度:#<BigDecimal:7f86aadf5f60,'0.23522219E1',18(45)>, post_id: nil, created_at: "2012-12-01 17:27:33", updated_at: "2012-12-01 17:27:33",注释:无>]位置负载 (0.2ms) SELECT "locations".* FROM "locations" WHERE "locations"."name" = 'Los Angeles' LIMIT 1在 97 毫秒内完成 500 个内部服务器错误ActiveModel::MassAssignmentSecurity::Error(无法批量分配受保护的属性:_destroy):app/models/location.rb:23:in `block in find_or_initialize_location_set'app/models/location.rb:21:in `each'app/models/location.rb:21:in`find_or_initialize_location_set'app/controllers/blogit/posts_controller.rb:79:in `create'
关于如何调整它以像以前一样在参数中包含删除函数的任何建议,但因此我没有收到 Can't mass-assign protected attributes: _destroy
错误?>
谢谢!
你可以这样做:
def self.find_or_initialize_location_set(location_set)#创建一个位置数组位置 = []位置=locations.delete_if { |元素|elem.flatten.empty?}location_set.each do |key, location|如果 location.delete(:_destroy) == "1"location.delete_if {|elem|elem[:name] == location[:name]}别的位置<
在关联的哈希上调用 delete
将修改哈希并返回您要删除的值(如果不存在,则返回 nil
).
或者,如果您需要在初始化位置进行删除,您可以在那里进行:
def find_or_initialize_by_name_with_delete(location)如果 location.delete(:_destroy)do_delete结尾find_or_initialize_by_name(位置)结尾
I have a function which splits nested attributes into an array so they can be checked for uniqueness and then saves them. The problem is, i'm getting a Can't mass-assign protected attributes: _destroy
error because the nested_form gem
inserts a hidden_field to highlight which attributes should be deleted.
Here's the *Posts_Contoller.rb* create function:
def create
location_set = params[:post].delete(:locations_attributes) unless params[:post][:locations_attributes].blank?
@post = current_blogger.blog_posts.new(params[:post])
@post.locations = Location.find_or_initialize_location_set(location_set) unless location_set.nil?
if @post.save
redirect_to @post, notice: 'Blog post was successfully created.'
else
render action: "new"
end
end
Here's the function in location.rb:
def self.find_or_initialize_location_set(location_set)
#create a locations array
locations = []
locations = locations.delete_if { |elem| elem.flatten.empty? }
location_set.each do |key, location|
locations << find_or_initialize_by_name(location)
end
locations
end
And here's the log:
Started POST "/blog/posts" for 127.0.0.1 at 2012-12-03 17:31:54 +0000
Processing by Blogit::PostsController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"r74iCzC4tJgVI6FiCEH7XzfiTmaqKihF5JSs7Ow3MSI=", "post"=>{"title"=>"This
is a post about Paris", "body"=>"This is a post about ParisThis is a post about Paris
This is a post about ParisThis is a post about Paris", "tag_list"=>"",
"locations_attributes"=>{"0"=>{"_destroy"=>"false", "name"=>"Paris", "longitude"=>"2.3522219", "latitude"=>"48.856614"}, "1354555760002"=>{"_destroy"=>"false", "name"=>"Los Angeles", "longitude"=>"-118.5155901", "latitude"=>"3
4.03563310000001"}}}, "_wysihtml5_mode"=>"1", "name"=>"losds", "legname"=>"Los Angeles", "longitude"=>"-118.5155901", "latitud
e"=>"34.03563310000001", "commit"=>"Submit"}
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
ActsAsTaggableOn::Tag Load (0.1ms) SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" W
HERE "taggings"."taggable_id" IS NULL AND "taggings"."taggable_type" = 'Blogit::Post' AND (taggings.context = 'tags' AND taggings.tagger_id IS NULL)
Location Load (0.1ms) SELECT "locations".* FROM "locations" WHERE "locations"."name" = 'Paris' LIMIT 1
LOGGER LOCATIONS ARE[#<Location id: 768, name: "Paris", latitude: #<BigDecimal:7f86aadf6230,'0.48856614E2',18(45)>, longitude:
#<BigDecimal:7f86aadf5f60,'0.23522219E1',18(45)>, post_id: nil, created_at: "2012-12-01 17:27:33", updated_at: "2012-12-01 17
:27:33", notes: nil>]
Location Load (0.2ms) SELECT "locations".* FROM "locations" WHERE "locations"."name" = 'Los Angeles' LIMIT 1
Completed 500 Internal Server Error in 97ms
ActiveModel::MassAssignmentSecurity::Error (Can't mass-assign protected attributes: _destroy):
app/models/location.rb:23:in `block in find_or_initialize_location_set'
app/models/location.rb:21:in `each'
app/models/location.rb:21:in `find_or_initialize_location_set'
app/controllers/blogit/posts_controller.rb:79:in `create'
Any suggestions for how I can adapt this to include the delete function in the params as before but so I don't get the Can't mass-assign protected attributes: _destroy
error?
Thanks!
You could do something like:
def self.find_or_initialize_location_set(location_set)
#create a locations array
locations = []
locations = locations.delete_if { |elem| elem.flatten.empty? }
location_set.each do |key, location|
if location.delete(:_destroy) == "1"
locations.delete_if {|elem| elem[:name] == location[:name]}
else
locations << find_or_initialize_by_name(location)
end
end
locations
end
Calling delete
on the associated hash will modify the hash and return the value you're deleting (or nil
if not present).
Or if you need the delete on the initialized location, you could do it there:
def find_or_initialize_by_name_with_delete(location)
if location.delete(:_destroy)
do_delete
end
find_or_initialize_by_name(location)
end
这篇关于如何修复此创建功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!