# Upload an image

**URL:** <https://www.ruby-forum.com/t/upload-an-image/57381>\
**Category:** Rails\
**Created:** [April 10, 2006, 3:40pm UTC](https://www.ruby-forum.com/t/upload-an-image/57381 "2006-04-10T15:40:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![taq](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7c8e57/32.png) [@taq](https://www.ruby-forum.com/u/taq)\
**Post date:** [April 10, 2006, 3:40pm UTC](https://www.ruby-forum.com/t/upload-an-image/57381/1 "2006-04-10T15:40:59Z")

</div>

Hello there. 🙂

I have this view:

\<%= form\_tag({:action =\> ‘create’}, :multipart =\> true) %\>

| User | \<%= text\_field("user","name") %\> |
| Image | \<%= file\_field("user","img") %\> |

 \<%= submit\_tag "Create new user", :class =\> "submit" %\> \<%= end\_form\_tag %\>

I use this form to upload an image. On the controller I have:

def create  
params[:user][:img] = params[:user][:img].read  
@user = User.new(params[:user])

```
  if @user.save
  ...

```

It reads the content of the file, saves it ok, but two questions:

First, is it ok to read on the controller? I saw samples on both the  
controller and the model (on the model it just did not worked for me -  
using img\_field.read gives an error telling me

undefined method `read’ for #String:0xb651c3d4

Second, how, with just those two fields, with no other field on the form  
with content-type and so on (I saw some samples with that), make sure  
that the user will only upload, for example, PNG files? I made some code  
on the controller, but is there a way to do that on the model?

I saw on “Agile Web D. with Rails” that they used a

validates\_format\_of :content-type, :with =\> /^image/

on the model, but as I don’t want to have a content\_type field (and  
don’t want to), is there a way do validate this?

Thanks!

---

<div class="post-metadata">

**Author:** ![taq](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7c8e57/32.png) [@taq](https://www.ruby-forum.com/u/taq)\
**Post date:** [April 10, 2006, 4:15pm UTC](https://www.ruby-forum.com/t/upload-an-image/57381/2 "2006-04-10T16:15:07Z")

</div>

I haven’t read you code, but why don’t you use the file\_column plugin ?  
It  
just works and solves a lot of problems related to image upload

---

<div class="post-metadata">

**Author:** ![taq](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7c8e57/32.png) [@taq](https://www.ruby-forum.com/u/taq)\
**Post date:** [April 10, 2006, 4:28pm UTC](https://www.ruby-forum.com/t/upload-an-image/57381/3 "2006-04-10T16:28:42Z")

</div>

> It  
> just works and solves a lot of problems related to image upload

Hi!

But does it handle the content-type checking?

Best regards,

---

<div class="post-metadata">

**Author:** ![taq](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7c8e57/32.png) [@taq](https://www.ruby-forum.com/u/taq)\
**Post date:** [April 10, 2006, 11:25pm UTC](https://www.ruby-forum.com/t/upload-an-image/57381/4 "2006-04-10T23:25:35Z")

</div>

Yes, it does handle all content type validations. Its handy!

Regards,  
Rizwan
