【问题标题】:Paperclip Jcrop and Rails 4 - infinite loop fixPaperclip Jcrop 和 Rails 4 - 无限循环修复
【发布时间】:2013-11-30 17:36:18
【问题描述】:

试图让它在 Rails 4 中工作时遇到一些麻烦 - http://railscasts.com/episodes/182-cropping-images?view=comments

根据 cmets 中的一个问题:使用 after_update 回调更新图像,它陷入了无限循环

显然解决方法是把 @user.avatar.reprocess!而是直接在控制器中。但是我不确定这应该在控制器中的确切位置。如果我把它放在正确的位置,它是否可以与 rails 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

【问题讨论】:

    标签: ruby-on-rails paperclip jcrop


    【解决方案1】:

    您可以通过Paperclip - Issue #866阅读有关此问题的更多信息。

    用户 jgarber 描述了一种解决方法,使用以下方法:

      def reprocess_photo
        photo.assign(photo)
        photo.save
      end
    

    【讨论】:

      【解决方案2】:

      我的更新操作:

      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
      

      这对我很有效。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-07
        • 2014-10-17
        • 2015-03-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多