Paperclip Jcrop and Rails 4 - Endless Loop Fix

Some problems trying to get this to work in Rails 4 - http://railscasts.com/episodes/182-cropping-images?view=comments

According to one of the questions in the comments: using the after_update callback to update the images, it ran into an infinite loop

Apparently the fix is ​​to install @ user.avatar.reprocess! directly in the controller . However, I'm not sure exactly where in the controller this should go. And if I put this in the right place, will it work with rails 4?

I tried the following with no luck:

def create @user = User.new(user_params) if @user.save if user_params[:avatar].blank? @user.avatar.reprocess! flash[:notice] = "Successfully created user." redirect_to @user else render :action => "crop" end else render 'new' end end def update @user = User.find(params[:id]) if @user.update_attributes(user_params) if user_params[:avatar].blank? @user.avatar.reprocess! flash[:notice] = "Successfully updated user." redirect_to @user else render :action => "crop" end else render :action => 'edit' end end 
+7
ruby-on-rails jcrop paperclip
source share
2 answers

my update action:

 def update @user=User.find(params[:id]) @user.update_attributes(user_params) if @user.cropping? @user.profile_image.reprocess! end if @user.save! redirect_to user_path(@user) end end 

and my cropper.rb

  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 

It worked beautifully for me.

0
source share

You can learn more about this through the β€œPaperclip” - issue number 866 .

User jgarber describes hacking it using the following:

  def reprocess_photo photo.assign(photo) photo.save end 
0
source share

All Articles