Search code examples
ruby-on-railsruby-on-rails-3ruby-on-rails-3.2paperclipjcrop

Rails: Having issues redirecting after cropping image with Jcrop & paperclip


I am trying to implement Jcrop for Paperclip and I am 99% done. I am able to crop the image and store it. The issue is that after the image is cropped and stored it again takes me to the Crop action with the recently cropped image. So it is asking me to crop a cropped image. I am not sure how to get out of it. I'd like to go to user_path after the image is cropped not crop again. Here is the code in my update action:

  def update
    @user.update_attributes(params[:user])
    if @user.avatar_file_name.nil?
      redirect_to :back
    else
      if params[:user].blank?
        redirect_to user_path(@user)
      else
        render :action => 'crop'
        @user.avatar.reprocess! if :cropping?
      end
    end
  end

If I have this instead, then the image won't crop. It is stored as the original image and I am redirected to the user_path:

  def update
    @user.update_attributes(params[:user])
    if @user.avatar_file_name.nil?
      redirect_to :back
    else
      if params[:user][:avatar].blank?
        redirect_to user_path(@user)
      else
        render :action => 'crop'
        @user.avatar.reprocess! if :cropping?
      end
    end
  end

Solution

  • This solved my question. It is probably very specific and won't ever help anyone, but here is the answer anyway. Thanks @grotori for your time!

      def update
        @user.update_attributes(params[:user])
        if @user.avatar_file_name.nil?
          redirect_to :back
          elsif !params[:user][:crop_x].blank?
            @user.avatar.reprocess! 
            redirect_to edit_user_path(@user) and return
          else
            render :action => 'crop'
        end
      end