ruby-on-rails – Paperclip Jcrop和Rails 4 – 无限循环修复

试图让它在Rails 4中运行时遇到一些麻烦 –
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

这很适合我.

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...