0

I'm trying to remove an attribute from params before to update an object, but i don't succeed to do it.

Params contain an object Post, and i want to remove image_url from it :

{
 "post" : "{
     \"content\" : \"dfsgdfa\",
     \"created_at\" : \"2013-09-01T08:39:26Z\",
     \"id\" : 21,
     \"image_content_type\" : \"image/jpeg\",
     \"image_file_name\" : \"img.jpg\",
     \"image_file_size\" : 61140,
     \"image_updated_at\" : \"2013-09-01T08:39:26Z\",
     \"title\" : \"sdsdsdd\",
     \"updated_at\" : \"2013-09-01T08:39:26Z\",
     \"user_id\" : 4,
     \"image_url\" : \"/system/posts/images/000/000/021/original/img.jpg?137802476
  \"}",
  "image" : "null",
  "action" : "update",
  "controller" : "posts",
  "id" : "21"
}

So i did like that :

params[:post].delete("image_url")

No errors are raised, but image_url is still in params[:post]

How can i delete it ?

Joe Bow
  • 101
  • 1
  • 4
  • Try reject instead of delete, you may want to look up the documentation on both methods. – Matt Sep 02 '13 at 12:43
  • This is neither a string nor hash. It looks like a hash with `post`'s value a string but it's syntax error. The last line of post should be `\"},` instead of `\"}",` Please double check and post correct data. – Billy Chan Sep 02 '13 at 13:37

4 Answers4

1

You can pass a subset into whatever function you want with:

@object = Foo.new(params.reject{|key,value| key == 'image_url'})

This doesn't remove it entirely from params, but returns a copy without that key

DGM
  • 26,629
  • 7
  • 58
  • 79
0

params[:post] is actually string in your case. It's not a hash. That's why it is not working.

ventsislaf
  • 1,651
  • 12
  • 21
0

params[:post] is not a hash, when you inspect it over controller it will look like hash. It's actually ActiveSupport::HashWithIndifferentAccess instance.

You can not remove a key/value pair from a Hash using Hash#delete: you should to write before save callback

def delete_image_url
  params[:post].delete :image_url
end

I hope it will help you. I already tested it.

Pravin Mishra
  • 8,298
  • 4
  • 36
  • 49
-1

params['post'].delete("image_url")

mshiltonj
  • 316
  • 3
  • 14
  • No not working neither, like as said vencislaf post is not a hash but a string, but i don't know how to convert it – Joe Bow Sep 02 '13 at 12:48