M dunbavan
M dunbavan

Reputation: 1167

Why can I not upload an image with Paperclip

I am trying to create an image upload on a Devise user signup form and its not saving the form when I submit it. I have set it up as follows:

user.rb

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

  #has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"

  has_attached_file :avatar, styles: {
        large: "600x450#",
        medium: "250x250#",
        small: "100x100#"
    }, :default_url => "/images/:style/filler.png"
  validates_attachment_content_type :avatar, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]
  validates :avatar, presence: true
end

users_controller.rb

class UsersController < ApplicationController
    def index
        @users = User.order('created_at DESC').all
    end
    def create
      @user = User.create( user_params )
    end 
    private

    def user_params
      params.require(:user).permit(:avatar)
    end
end

/views/users/registrations/new.html.erb

<h2>Sign up yeah</h2>

<%= simple_form_for(resource, :as => resource_name, :html => { :multipart => true }, :url => registration_path(resource_name)) do |f| %>
  <%= f.error_notification %>

  <div class="form-inputs">
    <%= f.input :email, :required => true, :autofocus => true %>
    <%= f.input :password, :required => true %>
    <%= f.input :password_confirmation, :required => true %>
    <%= f.file_field :avatar %>
  </div>

  <div class="form-actions">
    <%= f.button :submit, "Sign up" %>
  </div>
<% end %>

<%= render "users/shared/links" %>

In my logs for development I get this error printed:

Started POST "/users" for 127.0.0.1 at 2014-08-01 15:15:06 +0100
Processing by Devise::RegistrationsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"v/lf0StTLDOtdGtBynnecKm4fVL92+j2N9vepCvAW4U=", "user"=>{"email"=>"[email protected]", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "avatar"=>#<ActionDispatch::Http::UploadedFile:0x00000102c02800 @tempfile=#<File:/var/folders/mf/srx7jt8s2rdg0mn5hr98cvz80000gn/T/RackMultipart20140801-1710-n7njjc>, @original_filename="2.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"user[avatar]\"; filename=\"2.png\"\r\nContent-Type: image/png\r\n">}, "commit"=>"Sign up"}
Command :: file -b --mime '/var/folders/mf/srx7jt8s2rdg0mn5hr98cvz80000gn/T/c81e728d9d4c2f636f067f89cc14862c20140801-1710-1mj70uf.png'
Command :: identify -format '%wx%h,%[exif:orientation]' '/var/folders/mf/srx7jt8s2rdg0mn5hr98cvz80000gn/T/c81e728d9d4c2f636f067f89cc14862c20140801-1710-di3eje.png[0]' 2>/dev/null
[paperclip] An error was received while processing: #<Paperclip::Errors::NotIdentifiedByImageMagickError: Paperclip::Errors::NotIdentifiedByImageMagickError>
Command :: identify -format '%wx%h,%[exif:orientation]' '/var/folders/mf/srx7jt8s2rdg0mn5hr98cvz80000gn/T/c81e728d9d4c2f636f067f89cc14862c20140801-1710-di3eje.png[0]' 2>/dev/null
[paperclip] An error was received while processing: #<Paperclip::Errors::NotIdentifiedByImageMagickError: Paperclip::Errors::NotIdentifiedByImageMagickError>
Command :: identify -format '%wx%h,%[exif:orientation]' '/var/folders/mf/srx7jt8s2rdg0mn5hr98cvz80000gn/T/c81e728d9d4c2f636f067f89cc14862c20140801-1710-di3eje.png[0]' 2>/dev/null
[paperclip] An error was received while processing: #<Paperclip::Errors::NotIdentifiedByImageMagickError: Paperclip::Errors::NotIdentifiedByImageMagickError>
  [1m[35m (0.3ms)[0m  begin transaction
  [1m[36mUser Exists (0.3ms)[0m  [1mSELECT 1 AS one FROM "users" WHERE "users"."email" = '[email protected]' LIMIT 1[0m
Command :: file -b --mime '/var/folders/mf/srx7jt8s2rdg0mn5hr98cvz80000gn/T/c81e728d9d4c2f636f067f89cc14862c20140801-1710-1dimcon.png'
  [1m[35m (0.3ms)[0m  rollback transaction

I have imagemagick installed on my machine and working fine but I do not understand this error at all.

Can anyone help?

Upvotes: 1

Views: 393

Answers (1)

Kirti Thorat
Kirti Thorat

Reputation: 53048

As you are using Devise as an authentication solution within your application, Devise's own controllers would be used to perform all the action unless specified otherwise. If you notice the server log

Processing by Devise::RegistrationsController#create as HTML

the request for user creation is being handled by Devise's RegistrationsController and not UsersController.Its advisable to create your own controller for Devise model ONLY when you need some extreme customizations.

In your case, what you need to do is whitelist the avatar attribute so Devise knows that you are intending to save it in database.

The best approach to do this is via an initializer

# config/initializers/devise_permitted_parameters.rb

module DevisePermittedParameters
  extend ActiveSupport::Concern

  included do
    before_action :configure_permitted_parameters
  end

  protected

  def configure_permitted_parameters
    ## To permit attributes while registration i.e. sign up 
    devise_parameter_sanitizer.for(:sign_up) << :avatar
    ## To permit attributes while editing a registration 
    devise_parameter_sanitizer.for(:account_update) << :avatar
  end

end

DeviseController.send :include, DevisePermittedParameters

-OR-

Another approach is to update the ApplicationController as follows:

class ApplicationController < ActionController::Base
  #... 
  ## Add a callback
  before_action :configure_permitted_parameters, if: :devise_controller?

  protected

  def configure_permitted_parameters
    ## To permit attributes while registration 
    devise_parameter_sanitizer.for(:sign_up) << :avatar

    ## To permit attributes while editing a registration 
    devise_parameter_sanitizer.for(:account_update) << :avatar
  end
end

The drawback of this approach is that it adds an extra overhead because each and every request to your application would be evaluated to determine if it is for a Devise Controller.

NOTE: Just to get a better idea of using Devise with Rails, I would suggest you to checkout an awesome Rails and Devise example application by Daniel Kehoe.

UPDATE

Run which convert command to get the path where ImageMagick is installed.

For example, if it returns /usr/local/bin/convert then, for development mode, add the following line into config/environments/development.rb:

Paperclip.options[:command_path] = "/usr/local/bin/"

Upvotes: 1

Related Questions