我该如何修复这个创建功能?

时间:2012-12-03 17:51:03

标签: ruby-on-rails ruby ruby-on-rails-3

我有一个函数可以将嵌套属性拆分成一个数组,以便检查它们的唯一性,然后保存它们。问题是,我收到Can't mass-assign protected attributes: _destroy错误,因为nested_form gem插入了hidden_​​field以突出显示应删除哪些属性。

这是* Posts_Contoller.rb *创建功能:

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

这是 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

这是日志:

 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'

有关我如何调整此内容的任何建议,包括以前的参数中的删除函数,但所以我没有得到Can't mass-assign protected attributes: _destroy错误?

谢谢!

2 个答案:

答案 0 :(得分:3)

您可以通过将此错误添加到您的位置模型来消除此错误:

# NOT PERSISTENT
attr_accessor :_destroy
# ... 
attr_accessible :_destroy

如果没有这个,_destroy属性会受到mass assignment的保护,当您尝试为其赋值时,会抛出异常。

答案 1 :(得分:0)

您可以执行以下操作:

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

在关联的哈希上调用delete将修改哈希并返回您要删除的值(如果不存在,则返回nil。)

或者,如果您需要在初始化位置删除,您可以在那里执行:

def find_or_initialize_by_name_with_delete(location)
  if location.delete(:_destroy)
    do_delete
  end

  find_or_initialize_by_name(location)
end