Paperclip Jcrop and Rails 4 - infinite loop fix
Asked Answered
H

2

7

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

As per 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 put @user.avatar.reprocess! directly in the controller instead. However I am not sure where exactly in the controller this should go. And if I'm putting this in the right place is it going to work with rails 4?

I have 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
Hysterogenic answered 17/11, 2013 at 16:17 Comment(0)
H
0

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

This has worked for me nicely.

Hubblebubble answered 15/3, 2014 at 5:10 Comment(0)
L
0

You can read more about this issue via Paperclip - Issue #866.

User jgarber describes a hack to workaround it, using the following:

  def reprocess_photo
    photo.assign(photo)
    photo.save
  end
Lublin answered 24/6, 2015 at 16:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.