# Uploading photos

**URL:** <https://www.ruby-forum.com/t/uploading-photos/195655>\
**Category:** Rails\
**Created:** [September 27, 2010, 6:06pm UTC](https://www.ruby-forum.com/t/uploading-photos/195655 "2010-09-27T18:06:36Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![sprsquish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d9b06d/32.png) [@sprsquish](https://www.ruby-forum.com/u/sprsquish)\
**Post date:** [September 27, 2010, 6:06pm UTC](https://www.ruby-forum.com/t/uploading-photos/195655/1 "2010-09-27T18:06:36Z")

</div>

Hi all,

Learning RoR (and loving it!) to develop a website. A requirement of the  
website is enabling users to upload photos to be made viewable  
(dynamically) on the website. Is this something I can develop with RoR?

Thanks,  
Jeff

---

<div class="post-metadata">

**Author:** ![sprsquish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d9b06d/32.png) [@sprsquish](https://www.ruby-forum.com/u/sprsquish)\
**Post date:** [September 27, 2010, 6:56pm UTC](https://www.ruby-forum.com/t/uploading-photos/195655/2 "2010-09-27T18:56:43Z")

</div>

Piece of cake… be sure to check out ‘rails paperclip’ via google. Tons  
of useful examples.

---

<div class="post-metadata">

**Author:** ![sprsquish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d9b06d/32.png) [@sprsquish](https://www.ruby-forum.com/u/sprsquish)\
**Post date:** [September 27, 2010, 7:36pm UTC](https://www.ruby-forum.com/t/uploading-photos/195655/3 "2010-09-27T19:36:08Z")

</div>

with rails is a dream , i have an example app that i will update tonight  
,  
is for new comers and is well commented. if you want to i can give you  
the  
link. It will be ready by tomorrow.

---

<div class="post-metadata">

**Author:** ![sprsquish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d9b06d/32.png) [@sprsquish](https://www.ruby-forum.com/u/sprsquish)\
**Post date:** [September 27, 2010, 8:33pm UTC](https://www.ruby-forum.com/t/uploading-photos/195655/4 "2010-09-27T20:33:52Z")

</div>

Jeff S. wrote:

> Hi all,
> 
> Learning RoR (and loving it!) to develop a website. A requirement of the  
> website is enabling users to upload photos to be made viewable  
> (dynamically) on the website. Is this something I can develop with RoR?

I’ve never done it, but lots of people have. All of them recommend the  
Paperclip plugin. I think that’s the one point of complete unanimity in  
the Rails community. 🙂

> Thanks,  
> Jeff

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![sprsquish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d9b06d/32.png) [@sprsquish](https://www.ruby-forum.com/u/sprsquish)\
**Post date:** [September 28, 2010, 2:17am UTC](https://www.ruby-forum.com/t/uploading-photos/195655/5 "2010-09-28T02:17:45Z")

</div>

As a learner I have and still looking to find that same solution.  
paperclip is a full blown gem. To a student it would be profitable if  
anyone who knows how to do it in purely rails, would write a small  
example.

Say I have a form that includes submitting to other Models as well.  
How do I do that?

jobs/new.html.erb  
\<% form\_for [@job], :html =\> {:multipart =\> true } do |f| %\>  
\<%= f.error\_messages %\>

\<%= f.label "title" %\> \<%= f.text\_field :title %\>   
"Senior Ruby Developer" or "HTML5 CSS3 Guru" 

```
<dt><%= f.label "Location" %></dt>
  <dd>
  <%= f.text_field :location %>
  <br>
  <span class="hint">"Lincoln, UK", "Tallinn, Estonia", or

```

“Anywhere”

Category

\<% Catergory.all.each do |category| %\>

```
<dd>
  <label><%= f.radio_button :catergory_id, category.id %> <%=

```

category.name.titleize %\>

\<% end -%\>

```
<dt class="tarea"><%= f.label "Job descripton" %></dt>
  <dd class="tarea"><%= f.text_area :description, :size => "70x20"

```

%\>

```
<dt class="tarea"><%= f.label "How you should apply" %></dt>
  <dd class="tarea">

  <%= f.text_area :apply, :size => "70x6" %>
  <br>
  <span class="hint">Example: Send your portfolio to

```

removed\_email\_address@domain.invalid

## Now then, some`ing about your company. What should they know? \<%= f.label "Name" %\> \<%= f.text\_field :company %\> Example: 'Safarista Design', 'Safrista Jobs' or '37signals' ``` <dt><%= f.label :logo %></dt> <dd> <%= f.file_field("upload") %> <br> <span class="hint"> Optional &rarr; Your company logo will appear at the top of youur ``` listing. 190px wide is optimal. If larger we will resize it automatically. ``` <dt><%= f.label :url %></dt> <dd> <%= f.text_field :url %> <br> <span class="hint"> Ex: http://www.safarista.com </span> ``` ``` <dt><%= f.label :email %></dt> <dd> <%= f.text_field :email %> <br> <span class="hint"> This is where we will send you a confirmation receipt of your ``` payment and listing. \<%= f.submit 'Step 2: Proceed to preview your ad →' %\>

\<% end %\>

My Models are:

Job  
belongs\_to :catergory

# UPLOAD a file for the logo

def upload  
uploaded\_io = params[:job][:logo]  
File.open(Rails.root.join(‘public’, ‘images’,  
uploaded\_io.original\_filename), ‘w’) do |file|  
file.write(uploaded\_io.read)  
end  
end  
end

Catergory  
has\_many :jobs

This is still not working, the image does not upload. Why? Please help  
in the simple inbuilt Rails. Am using v2.3.8

---

<div class="post-metadata">

**Author:** ![sprsquish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d9b06d/32.png) [@sprsquish](https://www.ruby-forum.com/u/sprsquish)\
**Post date:** [September 28, 2010, 4:22pm UTC](https://www.ruby-forum.com/t/uploading-photos/195655/6 "2010-09-28T16:22:28Z")

</div>

> **[\#134 Paperclip - RailsCasts](http://railscasts.com/episodes/134-paperclip)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.

to upload files without paperclip, you have to save the image details in  
the  
db and create an attribute accessor

def uploaded\_picture=(picture\_field)  
self.name =  
File.basename(picture\_field.original\_filename).gsub(/^\w.\_-/,  
’ ')  
self.content\_type = picture\_field.content\_type.chomp  
self.data = picture\_field.read  
end

but i should warn you that it makes no sense to reinvent the wheel , and  
there are many dangers for doing it the wrong way

---

<div class="post-metadata">

**Author:** ![sprsquish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d9b06d/32.png) [@sprsquish](https://www.ruby-forum.com/u/sprsquish)\
**Post date:** [September 28, 2010, 4:15pm UTC](https://www.ruby-forum.com/t/uploading-photos/195655/7 "2010-09-28T16:15:32Z")

</div>

I would think it is much simpler…

Be sure you have ImageMagick installed (or an alternative)  
Be sure your migration defined the logo\_file\_name, logo\_content\_type,  
logo\_file\_size in the jobs table.

My example:

class Stage \< ActiveRecord::Base

# cycling race stage

# belongs to a race-year, like 2010 - Vuelta d’Espana

# has many stage standings (rider finish order)

# has attached images for the route

# map and the elevation profile

belongs\_to :raceyear  
has\_many :stagestandings

has\_attached\_file :map, :styles =\> {:thumb =\> ‘100x100\>’}  
has\_attached\_file :profile, :styles =\> {:thumb =\> ‘100x100\>’}  
end

\<%= form\_for @stage, :html =\> {:multipart =\> true} do |f| %\>

blah blah blah, fields to select raceyear, enter the stage name,  
description, terrain and distance, relevant image upload fields  
below

\<%= f.label :map %\> \<%= f.file\_field :map %\>

\<%= f.label :profile %\> \<%= f.file\_field :profile %\>

\<%= f.submit %\> \<% end %\>

Paperclip does all the heavy lifting.

---

<div class="post-metadata">

**Author:** ![sprsquish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d9b06d/32.png) [@sprsquish](https://www.ruby-forum.com/u/sprsquish)\
**Post date:** [September 28, 2010, 5:08pm UTC](https://www.ruby-forum.com/t/uploading-photos/195655/8 "2010-09-28T17:08:32Z")

</div>

Mr. Winnymann wrote:

> As a learner I have and still looking to find that same solution.  
> paperclip is a full blown gem. To a student it would be profitable if  
> anyone who knows how to do it in purely rails, would write a small  
> example.

You have an example: the Paperclip source code.

And if you’re a beginner, then you definitely should not be trying to  
rewrite Paperclip.

BTW, you shouldn’t be using

 for your forms; they’re not  
semantically definition lists.

Best,  
–Â  
Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
