28

When I try to upload image using Paperclip gem I got this error:

NoMethodError (undefined method `stringify_keys' for <ActionDispatch::Http::UploadedFile:0x000000025387f0>)

class MenuItem < ActiveRecord::Base
 has_one :image


end

class Image < ActiveRecord::Base
 belongs_to :menu_item
 has_attached_file :image, :styles => {
            :large => "640x480",
            :medium => "300x300", 
            :thumb => "100x100" 
           }
end
Ryan Bigg
  • 106,965
  • 23
  • 235
  • 261
user908798
  • 688
  • 4
  • 12
  • 16

3 Answers3

74

I've seen this error happen before, usually when people attempt to call update_attributes like this:

update_attributes(params[:image])

The call should actually be this:

update_attributes(:image => params[:image])

A bit of a shot in the dark, but if that's it I'm sure we'll all be impressed.

Ryan Bigg
  • 106,965
  • 23
  • 235
  • 261
  • 1
    This did the trick for me as well, except I was using build() instead of update_attributes(). Thanks :) – fholgado Feb 22 '12 at 21:11
  • @RyanBigg can you elaborate on the problem?I am having a similar issue with uploading a zip file which I create through Submission.uploaded_file = params[:submission]. Stringify Keys only happens when I have the CanCan gem turned on though. – BookOfGreg Aug 27 '12 at 18:37
  • 1
    Could the OP mark this as the correct answer? This fixed my issue as well. – battlemidget Feb 01 '13 at 18:22
  • @Hamdan: OMG YES!!!1one. Paperclip expects all the attributes for the image to be sent through as an 'image' attribute. What the OP was originally doing was not that. – Ryan Bigg Apr 27 '14 at 22:36
  • Thats not what im asking. I actually cant understand how (:image => params[:image]) is different from (params[:image]) since what is inside params[:image] is actually the image object that the :image use to inherit its status of 'image object'. Can anybody tell me? – Hamdan Apr 29 '14 at 12:18
1

After struggling for a while in rails 3.2.2 I managed to solve this in this manner

(image = Image.new(image: params[:image])).save

update_attributes(image: image)

Community
  • 1
  • 1
Sagish
  • 1,065
  • 8
  • 13
0

I just had this problem, and to clarify things a bit, update_attributes is different from update_attribute.

The following should work:

update_attributes(:image => params[:image])

or

update_attribute(:image, params[:image])

There you go! There are other issues with update_attributes related to attr_accesible, but either works.

bradmalloy
  • 21
  • 5