Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Dave Faliskie
Dave Faliskie
17,793 Points

How to crop an image with rails 4

I have used paperclip to allow a user to upload a picture. I want to allow the user to choose the area of the picture they want to be used in a square cropped version. Everything online points to the rails cast #182. However I couldn't get it to work. Is it because it is outdated? is there a better way to allow cropping of an image?

1 Answer

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

You could try this: https://github.com/rsantamaria/papercrop

I'm curious and will probably try using it myself as well.

Dave Faliskie
Dave Faliskie
17,793 Points

Yes I tried that and it seems to work. I can see the image select an area, and view a preview. The problem is I click save nothing happens. I'm almost positive it is the way I have my controller written, I'll post below what I have.

  def update
    respond_to do |format|
      if @profile_picture.update(profile_picture_params)
        format.html { render :action => "crop"}
        format.html { redirect_to edit_user_path(@current_user), notice: 'Profile picture was successfully updated.' }
        format.json { render :show, status: :ok, location: @profile_picture }
      else
        format.html { render :edit }
        format.json { render json: @profile_picture.errors, status: :unprocessable_entity }
      end
    end
  end

any Ideas on how to properly write the controller. The "crop" file is exactly as shown on the papercrop docs.

Thanks, Dave

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

Unfortunately I did not manage to implement this myself properly. It shows up, but doesn't save the newly cropped file. And the documentation is very scarce.

Dave Faliskie
Dave Faliskie
17,793 Points

Exactly my problem. Do you know how to make it work, or is there a better way to add this feature. Or is this a feature I should just forget about?

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

I don't have any solution for you at this moment. I may find it in the future and I will post it here if I do.