Rails Helper Function – file.read not working?

Posted: June 23, 2009 in Ruby on Rails
Tags: , ,

Probably one if the simplest mistake I have ever done. But got stucked
with this for an hour before I would figure out what was wrong.

Rails has got a very nice set of helper functions that would generate
the HTML for you. So to upload a file you would probably write something
like this in your view.

But with this if you will try to extract the file in your controller,

params[:model][:image].read will give an error:


NoMethodError (undefined method `read’ for “<file.jpg>”:String)

So how come the image file became a String? Well, if you have done a
silly mistake like me than you have forgotten to add the “multipart”
value in the form.

So simply add this to your form_for tag and it would work like a charm.

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s