SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2009
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problems implementing paperclip

    I'm building a site where users will be able to upload multiple pictures, so decided to use paperclip. Implemented following the Railscast, and browse field shows up on page but when I create (or edit) a user, I get an error that says photo must be set. Any ideas what I'm doing wrong?

    user.rb:
    Code:
    class User < ActiveRecord::Base
      validates_presence_of :fname, :lname, :city, :country, :email, :confirm
      has_many :photos
      has_attached_file :photo, :styles => { :small  => "200x200>", :thumb=> "100x100>" },
        :url  => "/images/userpics/:id/:style/:basename.:extension",
        :path => ":rails_root/public/images/userpics/:id/:style/:basename.:extension"
    
      validates_attachment_presence :photo
      validates_attachment_size :photo, :less_than => 5.megabytes
      validates_attachment_content_type :photo, :content_type => ['image/gif', 'image/png', 'image/x-png', 'image/jpeg', 'image/pjpeg', 'image/jpg']
    
    end
    new.html.erb:
    Code:
    <h3>Create Your Account</h3>
    
     <% form_for @album, :html => { :multipart => true } do |f| %>
      <%= render :partial => 'form', :locals => {:f => f} %>
    
     <%= f.error_messages %>
    
      <p>
        <%= f.label :fname %><br />
        <%= f.text_field :fname %>
      </p> ...
    show.html.erb:
    Code:
    <h2>Edit Your Profile <%= @user.fname %></h2>
    
    <%= image_tag @user.photo.url(:thumb) %>
    <p>
      <b>First:</b>
      <%=h @user.fname %>
    </p>
    
    <p>
      <b>Last:</b>
      <%=h @user.lname %>
    </p>
    
    <p>
      <b>City:</b>
      <%=h @user.city %>
    </p>

  2. #2
    SitePoint Evangelist
    Join Date
    Feb 2006
    Location
    Worcs. UK
    Posts
    404
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Where do you get the error? Can you post the exact wording?

    If you comment out the four lines starting "validates_" do the errors messages still occur (this should tell us if the error is a coding or a validation problem). To comment out the line, put a # at the start.

  3. #3
    SitePoint Enthusiast
    Join Date
    Mar 2009
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ReggieB View Post
    Where do you get the error? Can you post the exact wording?

    If you comment out the four lines starting "validates_" do the errors messages still occur (this should tell us if the error is a coding or a validation problem). To comment out the line, put a # at the start.
    When I commented out the validates lines, the upload worked. Thanks!! So it appears the problem lies there. Will fiddle around with content type and size, see if that does the trick.

    But how do I make it so the user can upload multiple pictures? When I edit, it replaces the existing with a new one.

    Thanks,

    Veronica

  4. #4
    SitePoint Addict
    Join Date
    Feb 2007
    Posts
    270
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by vhenry View Post
    But how do I make it so the user can upload multiple pictures? When I edit, it replaces the existing with a new one.
    That's what edit means. You'll need to create new photo for each additional one you want to post. One way is to set up an "add photo" button, which takes you to a "create photo" form for uploading a new photo. In the long run, you may actually want to make that a separate function on your profile screen, perhaps an "edit/add photos" button that takes you to a form that lets you select a photo to edit or add a new one, or perhaps make each photo on the profile page a link that when clicked lets you replace(edit) the photo and have a separate add photo button.

    I'm worried about the earlier validate error. The fact that it created the new photo, yet the validator didn't see the photo as existing worries me. I've not used paperclip before, so I'm going to have to get that and play around with it a while before I open my mouth on it.

  5. #5
    SitePoint Enthusiast
    Join Date
    Mar 2009
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think I'll go ahead and add the "add photo" button with a new form.

    If you come up with anything on the validation error, would love to hear.

    Thanks


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •