试图让它在Rails 4中运行时遇到一些麻烦 –
http://railscasts.com/episodes/182-cropping-images?view=comments
http://railscasts.com/episodes/182-cropping-images?view=comments
根据评论中的一个问题:使用after_update回调来更新图像,它遇到了无限循环
显然修复是放@ user.avatar.reprocess!而是直接在控制器中.但是我不确定控制器到底应该在哪里.如果我把它放在正确的位置它是否适用于导轨4?
我试过以下没有运气:
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
解决方法
我的更新动作:
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
和我的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
这很适合我.