Uploading photos

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

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

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.

Jeff Schmidt 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. :slight_smile:

Thanks,
Jeff

Best,

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 %>

  <dl>
    <dt><%= f.label "title" %></dt>
      <dd>
      <%= f.text_field :title %>
      <br>
      <span class="hint">"Senior Ruby Developer" or "HTML5 CSS3 Guru"</

    </dd>

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

  <dt>Category</dt>

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

    <dd>
      <label><%= f.radio_button :catergory_id, category.id %> <%=
category.name.titleize %></label>
    </dd>

  <% end -%>

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

    <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
studio@safarista.com</span>
    </dd>
  </dl>
<section class="company">
  <h2>Now then, some`ing about your company. What should they know?</
h2>

  <dl>
    <dt><%= f.label "Name" %></dt>
    <dd>
    <%= f.text_field :company %>
    <br>
    <span class="hint">Example: 'Safarista Design', 'Safrista Jobs' or
'37signals'</span>

  </dd>

    <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.
      <br>
      190px wide is optimal. If larger we will resize it automatically.
    </span>
  </dd>

    <dt><%= f.label :url %></dt>
    <dd>
    <%= f.text_field :url %>
    <br>
    <span class="hint">
      Ex: http://www.safarista.com
    </span>
  </dd>

    <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.
    </span>
  </dd>
  </dl>

  </section><!--.company-->
    <%= f.submit 'Step 2: Proceed to preview your ad &rarr;' %>

<% 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

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
<p>
<%= f.label :map %>
<%= f.file_field :map %>
</p><p>
<%= f.label :profile %>
<%= f.file_field :profile %>
</p>
<%= f.submit %>
<% end %>

Paperclip does all the heavy lifting.

http://railscasts.com/episodes/134-paperclip

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

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 <dl> for your forms; they're not
semantically definition lists.

Best,