views:

450

answers:

5

Hello stackoverflow.

I am trying to use Paperclip with my Rails app to add an avatar to a user but it won't save my image or update the database column when creating the user.

This is what the model looks like:

class User < ActiveRecord::Base
  has_attached_file :avatar

And the registerform in haml:

- form_for :user, @user, :url => { :action => "signup" }, :html => { :multipart => true } do |f|
 ...
 ...
      %li
        %div{:class => "header"} Profilepicture
        %div{:class => "input"}
          = f.file_field :avatar

And when I look in the log this is what is being passed to the "signup" action:

Parameters: {"commit"=>"Save", "action"=>"signup", "controller"=>"user/register",      "user"=>{"name"=>"Micke Lisinge", "birthmonth"=>"07", "password_confirmation"=>"[FILTERED]", "nickname"=>"lisinge", "avatar"=>#<File:/tmp/RackMultipart20100426-3076-1x04oxy-0>, "gen"=>"m", "birthday"=>"23", "password"=>"[FILTERED]", "birthyear"=>"1992", "email"=>"[email protected]"}}
[paperclip] Saving attachments.

Paperclip says it is saving the template but when I look in the public folder in my app it has created a system but the system folder is empty. So it seems like it isn't saving the picture to the folder. It gets handled by the form and saved in my /tmp folder.

Maybe you guys have any tips or know what this problem might be?

Thanks in advance, Micke.

A: 

First check, if path is correct for the created attachment. You can use avatar.path to determine that. if it is not returning correct path, may be someone is overriding the default paperclip path?

Also check, if public/system is writable by the user as which you are running the application server.

Hemant Kumar
running as root so it should be able to write
Micke
i get `can't convert nil into String` when using `@user.avatar.path`
Micke
Its almost always useful to post complete backtrace.
Hemant Kumar
But it doesnt even save to the database
Micke
A: 

Try setting the :path option

has_attached_file :avatar,
  :path => ':rails_root/public/system/:id.:extension'
GregMoreno
+1  A: 

I got it to work.

I had to add :avatar to attr_accessible in my user model.

Posting this here and hopes that it helps someone sometime :)

Thanks guys for your help

Micke
i have the same problem, i tried adding the attr_accessible but it still dosent save correctly and shows a missing message when i try load the picture. this is very frustrating
Mo
Try to set the :path option. It might help
Micke
A: 

Don't forget to set :multipart => true in your form declaration. This has bitten me once or twice.

evaneykelen
A: 
has_attached_file :asset, :url  => "/assets/:id/:style/:basename.:extension",
  :path => ":rails_root/public/assets/:id/:style/:basename.:extension"

FYI, This code actually saved my files in the root directory of my machine "/" as the :rails_root param was failing. This is on Rails 3.0.0.rc

nicholasklick