Topic: Paperclip not saving any images

Hey everyone,

I was hoping this would've been a 5 minute task, but again, my database is not getting the values, this time from paperclip.

I tried this on a test app on my Mac, and it works fine. Go figure! tongue The only difference is on My Mac I'm just using Mongrel, and SQLite for my DB. The main app I have on Ubuntu LTS 8.04 LTS, running on Apache2, mod_rails 2.2.5, with MySQL 5 for my DB.

I got Paperclip installed, generated the paperclip, and called it avatar, of which it associates to the User model.

In the User model I added:

has_attached_file :avatar, :url => "/avatars/users/:id/:basename.:extension", :path => ":rails_root/public/avatars/users/:id/:basename.:extension"
# Added the following lines to attr_accessible
attr_accessible [everything else], :avatar_file_name, :avatar_content_type, :avatar_file_size, :avatar_updated_at

Then in my form for new/update user, I have:

<% form_for @user, :html => { :multipart => true } do |f| %>

    <%= f.error_messages %>
    
    <%= image_tag @user.avatar.url, :width => "150px", :height => "150px" %>

    <table>
  
      <tr>
    
        <td>First Name</td>
        <td><%= f.text_field :first %></td>
      
      </tr>
      <tr>
    
        <td>Last Name</td>
        <td><%= f.text_field :last %></td>
  
      </tr>
      <tr>
    
        <td>Login</td>
        <td><%= f.text_field :login %></td>
  
      </tr>
      <tr>
    
        <td>E-mail Address</td>
        <td><%= f.text_field :email %></td>
  
      </tr>
      <tr>
        <td>Upload an avatar</td>
        <td><%= f.file_field :avatar %>
      </tr>
      <tr>
    
        <td>Password</td>
        <td><%= f.password_field :password %></td>
  
      </tr>
      <tr>
    
        <td>Confirm Password</td>
        <td><%= f.password_field :password_confirmation %></td>
  
      </tr>
      
      <% if admin? %>
      
      <tr>
    
        <td>Administrator Role</td>
        <td><%= f.text_field :admin %></td>
  
      </tr>
      
      <% else %>
      
      <% end %>
      

    </table>

    <p><%= f.submit 'Update Account' %></p>

Save everything, update the current user in this case, and no go. I look in my Users table in my DB, with a good ol' "select * from users where id = 1;", and all those fields are NULL as I expected.

Ok, so now I go look in log/development.log. I see:

Processing UsersController#update (for 70.89.247.75 at 2009-11-16 17:32:15) [PUT]
  Parameters: {"user"=>{"password_confirmation"=>"", "admin"=>"1", "last"=>"Brinkerhoff", "first"=>"Justin", "avatar"=>#<File:/tmp/RackMultipart.18774.1>, "password"=>"", "login"=>"justin", "email"=>"justinbrinkerhoff@gmail.com"}, "commit"=>"Update Account", "authenticity_token"=>"9z5P+7wcmujDCYkcl0M5OjuCPQPnpUF+1mYp2aBVIew=", "id"=>"1"}
  User Columns (77.4ms)   SHOW FIELDS FROM `users`
  User Load (84.0ms)   SELECT * FROM `users` WHERE (`users`.`id` = 1) 
WARNING: Can't mass-assign these protected attributes: avatar
  SQL (75.5ms)   BEGIN
  User Exists (77.5ms)   SELECT `users`.id FROM `users` WHERE (LOWER(`users`.`login`) = BINARY 'justin' AND `users`.id <> 1) LIMIT 1
  User Exists (82.1ms)   SELECT `users`.id FROM `users` WHERE (LOWER(`users`.`email`) = BINARY 'justinbrinkerhoff@gmail.com' AND `users`.id <> 1) LIMIT 1
[paperclip] Saving attachments.
  SQL (87.5ms)   COMMIT
Redirected to http://iloveorganic.co.uk:3000/users/1/edit
Completed in 628ms (DB: 638) | 302 Found [http://iloveorganic.co.uk/users/1]
  SQL (79.6ms)   SET NAMES 'utf8'
  SQL (76.3ms)   SET SQL_AUTO_IS_NULL=0

So it appears that Paperclip is not processing the uploads properly, or something. I've tried searching around for a solution, but couldn't quite find anything helpful for this strange issue...

Has anyone came across this in the past?

If anyone has some good insight on this, I'd be much appreciative. big_smile

Last edited by jmbrink26 (2009-11-16 22:37:45)

Re: Paperclip not saving any images

Hmm... trying to narrow down the possible cause of this issue.

I thought I'd try to first just boot Mongrel, and see what happens. Then I disabled the site from Apache, using a2dissite, then stopped Apache altogether.

From there, I booted Mongrel by itself, but still I get the exact same result looking at the SQL when it refers to Attachment, which says "avatar"=>#<File:/tmp/RackMultipart.[some number string]".

It just sounds to me like Passenger is still getting in the way, even though I had shut it off for the time being, and just used Mongrel.

I just don't get it.. :S

Re: Paperclip not saving any images

Ok, went a step farther, and created a little test app on my server, again using sqlite for the db. And again, it worked on the same server the app I'm having trouble with is on.

Again, I was just using Mongrel. Now going to convert the db to mysql and see what happens.

Re: Paperclip not saving any images

Ok, created the db to mysql. Worked fine after that. Even after setting up a VirtualHost for apache, and using Passenger, it worked fine.

Furthermore, with the problematic app, I changed the db to a local mysql instance, I also used mongrel again, I even then changed the db to a local sqlite3 db. In every scenario the same thing still happens.

Yet, in every other application I write it works fine using both Mongrel and Passenger, using mysql or sqlite3.

It just doesn't make sense. What am I not seeing here?

Re: Paperclip not saving any images

But you haven't made :avatar attr_accessible, according to this in your log:

WARNING: Can't mass-assign these protected attributes: avatar

So the upload file (the multipart file) can't be assigned, meaning Paperclip doesn't get a look in.

Not sure why that'd be different on different servers though.

Rob Anderton
TheWebFellas

Re: Paperclip not saving any images

LMAO OMG....

I put in :avatar_file_size, :avatar_content_type, and all those on the list of attr_accessible, and what it really needed was :avatar.

LOL ok, I get it now. Thanks Rob for helping me see that... tongue

That is weird though that it works different in different servers...

Re: Paperclip not saving any images

I found this thread useful, had made the same mistake. Thanks for the answer! smile
I agree that it is strange how erratically paperclip behaves here. You had inconsistencies based on the server, and I made one model without the attr_accessor that worked fine, then tried to use the same code for my user model and was really confused when it didn't work.

Re: Paperclip not saving any images

I also made the same mistake. Thanks so much for the answer!

Re: Paperclip not saving any images

I'm having the same problem, BUT i have the attr_accessible setted.

model:

class ImageDescription < QuestDescription
  has_attached_file :img
  attr_accessible :img
end

new.html.erb:

<%= semantic_form_for @imgD, :html => { :multipart => true } do |form| %>
  <%= form.input :img%>
  <%= form.actions %>
<%end%>

controller:

class ImageDescriptionsController < ApplicationController
  
  def new
    @imgD = ImageDescription.new
  end
  
  def create
    @imgD=ImageDescription.new(params[:imgD])
    if @imgD.save
      redirect_to :back, :flash => {:notice => "saved"} 
    else
      redirect_to :back, :flash => {:error => "error"} 
    end
  end
  
  def show
    @imgD=ImageDescription.find(params[:id])
  end
end

schema:

# == Schema Information
#
# Table name: quest_descriptions
#
#  id               :integer         not null, primary key
#  type             :string(255)
#  created_at       :datetime        not null
#  updated_at       :datetime        not null
#  img_file_name    :string(255)
#  img_content_type :string(255)
#  img_file_size    :integer
#  img_updated_at   :datetime
#

from the console i can save it:

ImageDescription.create(:img => File.new(Rails.root + "public/images/grid.png"))
    (0.1ms) begin transaction
    SQL (31.9ms) INSERT INTO "quest_descriptions" ("created_at", "img_content_type", "img_file_name", "img_file_size", "img_updated_at", "type", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?) ["created_at", Wed, 15 Feb 2012 00:07:48 UTC +00:00], ["img_content_type", "image/png"], ["img_file_name", "grid.png"], ["img_file_size", 206], ["img_updated_at", Wed, 15 Feb 2012 00:07:48 UTC +00:00], ["type", "ImageDescription"], ["updated_at", Wed, 15 Feb 2012 00:07:48 UTC +00:00] commit transaction
    => #

any guess? I'm getting an headache on this

Re: Paperclip not saving any images

This was very helpful. Thanks for posting!

Re: Paperclip not saving any images

i have solved changin

@imgD=ImageDescription.new(params[:imgD])

with

@imgD=ImageDescription.new(params[:image_description])

in the controller



(yes, very stupid mistake)