mrharrison
mrharrison

Reputation: 11

Ruby 1.87 Rails 3.03 and Devise. Paperclip image not being saved although it says it is in log file

I have gotten paperclip to work on my system without a problem. In this recent app that I have been building, for some reason I can't get paperclip to save the image, even though the log file says it is, it doesn't show up in the sql datase either.

The only difference that I have, is using Devise and wondering if there is some extra setup I need to change with it.

The log file says:

Started POST "/users/1" for 127.0.0.1 at Fri Dec 10 14:56:52 -0800 2010

Processing by UsersController#update as HTML Parameters: {"commit"=>"Update", "authenticity_token"=>"JexxammsT+VPzqwbLr/YohuY4vcpBFdfVrOwDjDQEUo=", "utf8"=>"✓", "id"=>"1", "user"=>{"avatar"=>#, @headers="Content-Disposition: form-data; name=\"user[avatar]\"; filename=\"Nice-Smile.jpg\"\r\nContent-Type: image/jpeg\r\n", @content_type="image/jpeg", @original_filename="Nice-Smile.jpg">}} User Load (3.1ms) SELECT "users".* FROM "users" WHERE ("users"."id" = 1) LIMIT 1 User Load (0.5ms) SELECT "users"."id" FROM "users" WHERE ("users"."email" = '[email protected]') AND ("users".id <> 1) LIMIT 1 [paperclip] Saving attachments. Redirected to http://localhost:3000/users/1 Completed 302 Found in 124ms

Started GET "/users/1" for 127.0.0.1 at Fri Dec 10 14:56:52 -0800 2010 Processing by UsersController#show as HTML Parameters: {"id"=>"1"} User Load (3.1ms) SELECT "users".* FROM "users" WHERE ("users"."id" = 1) LIMIT 1 CACHE (0.0ms) SELECT "users".* FROM "users" WHERE ("users"."id" = 1) LIMIT 1 Rendered users/_basic_info.html.erb (2.0ms) Rendered layouts/_stylesheets.html.erb (5.2ms) Rendered layouts/_header.html.erb (12.1ms) Rendered layouts/_footer.html.erb (2.4ms) Rendered users/show.html.erb within layouts/application (47.2ms) Completed 200 OK in 167ms (Views: 62.2ms | ActiveRecord: 6.6ms)

Started GET "/avatars/small/missing.png" for 127.0.0.1 at Fri Dec 10 14:56:53 -0800 2010

Started GET "/avatars/small/missing.png" for 127.0.0.1 at Fri Dec 10 14:56:53 -0800 2010

ActionController::RoutingError (No route matches "/avatars/small/missing.png"):

Here is my users controller:

  class UsersController < ApplicationController
  def home
    @user = User.find(params[:id])
  end

  def index
    @user = User.all
    @title = "All users"
  end

  def show
    @user = User.find(params[:id])
    @title = current_user.first_name
  end

  def create
    @user = User.new(params[:user])
    if @user.save
      flash[:notice] = "Profile Updated"
      redirect_to @user
    else
      render :action => 'new'
    end
  end

  def update
    @user = User.find(params[:id])
    if @user.update_attributes(params[:product])
      flash[:notice] = "Profile Updated"
      redirect_to @user
    else
      render :action => 'edit'
    end
  end


end

And here is my User Model:

  class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :lockable, :timeoutable and :activatable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :first_name, :last_name, :avatar
  attr_accessible :bio, :sex, :birthday, :facebook, :twitter, :hometown, :current_city, :interested_in, :looking_for

  validates :first_name,  :presence => true,  
                    :length   => { :maximum => 50 }

  validates :last_name,  :presence => true,  
                    :length   => { :maximum => 50 }

  has_attached_file :avatar, :styles => { :thumb  => "50x50#",
                                          :small  => "150x150>",
                                          :medium => "300x200>",
                                          :large  => "600x600>"   },
                    :url  => "/assets/users/:id/:basename/:style.:extension",
                    :path => ":rails_root/public/assets/users/:id/:basename/:style.:extension"


  validates_attachment_size :avatar, :less_than => 5.megabytes
  validates_attachment_content_type :avatar, :content_type => ['image/jpeg', 'image/jpg', 'image/png', 'image/gif']


end

I have tried many things over and over again, but not sure why the file isn't being saved. Thanks for any help in advance.

Upvotes: 1

Views: 1526

Answers (2)

Pavel K.
Pavel K.

Reputation: 6817

not sure if it's related, but just noticed the line

if @user.update_attributes(params[:product])

in your code. was that the reason?

Upvotes: 0

Claudio Escudero
Claudio Escudero

Reputation: 21

You put the tag html => {:multipart => true} in your form?

Example:

<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :method => :put, :multipart => true }) do |f| %>
  <%= devise_error_messages! %>
  <div class="blockForm">
    <p>
      <%= f.label :name %>
      <%= f.text_field :name %>
    </p>

    <p>
      <%= f.label :email %>
      <%= f.text_field :email %>
    </p>
    <p>
      <%= f.label :avatar %>
      <%= f.file_field :avatar %>
    </p>
    <p>
      <%= f.label :password %>
      <%= f.password_field :password %>
      <span>Deixe em branco caso não queira alterar a senha</span>
    </p>

    <p>
      <%= f.label :password_confirmation %>
      <%= f.password_field :password_confirmation %>
    </p>

    <p>
      <%= f.label :current_password %>
      <%= f.password_field :current_password %>
      <span>Coloque a sua senha para salvar as alterações</span>
    </p>
  </div>
  <p><%= f.submit "Atualizar", :class => 'button' %></p>
<% end %>

Upvotes: 2

Related Questions