RailsCasts Pro episodes are now free!

Learn more or hide this

Kassymov Shyngys's Profile

GitHub User: chika-kasymov

Comments by Kassymov Shyngys

Avatar

Hello, I'm new in rails and I spend 4 hours to get it work :)

My addidions to this tutorial code:

In user model add

ruby
attr_accessible :avatar, :crop_x, :crop_y, :crop_w, :crop_h

In my controller (I'm using device, so in application_controller.rb)

ruby
class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception

  before_action :configure_devise_permitted_parameters, if: :devise_controller?

  protected

  def configure_devise_permitted_parameters
    registration_params = [:avatar, :name, :email, :password, :password_confirmation]
    update_params = [:avatar, :name, :email, :password, :password_confirmation, :crop_x, :crop_y, :crop_w, :crop_h]

    if params[:action] == 'update'
      devise_parameter_sanitizer.for(:account_update) { 
        |u| u.permit(update_params)
      }
    elsif params[:action] == 'create'
      devise_parameter_sanitizer.for(:sign_up) { 
        |u| u.permit(registration_params) 
      }
    end
  end

end

Replace reprocess_avatar method with this

ruby
def reprocess_avatar
  avatar.assign(avatar)
  avatar.save
end

And finally replace cropper.rb as said before on this thread

ruby
module Paperclip
  class Cropper < Thumbnail
    def transformation_command
      if crop_command
        crop_command + super.join(' ').sub(/ -crop \S+/, '').split(' ') # super returns an array like this: ["-resize", "100x", "-crop", "100x100+0+0", "+repage"]
      else
        super
      end
    end

    def crop_command
      target = @attachment.instance
      if target.cropping?
        ["-crop", "#{target.crop_w}x#{target.crop_h}+#{target.crop_x}+#{target.crop_y}"]
      end
    end
  end
end

May be it'll help somebody.