Locked lesson.
About this lesson
Using Paperclip and ImageMagick to handle image uploading by users. Part 1.
Exercise files
Download this lesson’s related exercise files.
Paperclip and ImageMagick Part 1.docx59 KB Paperclip and ImageMagick Part 1 - Solution.docx
59.6 KB
Quick reference
Paperclip and ImageMagick Part 1
In this video we'll begin to install Paperclip and ImageMagick.
When to use
Paperclip is a Gem that handles image uploading. ImageMagick is an image processing app that handles image resizing and things like that.
Instructions
Add the Paperclip gem to your gemfile and run bundle install as always.
Install ImageMagick to the command line terminal in two steps:
sudo apt-get update
sudo apt-get install imagemagick -y
Next: Add these lines of code to our app/models/pin.rb file:
has_attached_file :image, styles: { medium: "300x300>", thumb: "100x100>" }
validates_attachment_content_type :image, content_type: /\Aimage\/.*\z/
Next: Create a migration for paperclip:
rails generate paperclip pin image
Next: Push our new migration to the local and production server with rake db:migrate and heroku run rake db:migrate as always.
Hints & tips
- ImageMagick is not a Gem, it's a command line terminal program
- Don't forget to update your apt-get terminal program before installing imagemagick
- Add the paperclip reference to your pin.rb file
- Create a paperclip migration with rails generate
- 00:05 Now, it's time to dive into the actual fun stuff and
- 00:06 start building out the ability for people to upload images to our app.
- 00:08 So we're gonna use something called Paperclip, and that's a RubyGem,
- 00:13 go to rubygems.org like always, search for paperclip, and go ahead and
- 00:17 copy the gem file, reference, head back over and paste it in and save it.
- 00:22 Now, before we run our bundle install command,
- 00:25 I wanna look at the documentation real quick, 'cause this is a little bit different,
- 00:28 this requires something else before you install it.
- 00:31 It has a requirement and it's this Image Processor thing called Imagemagick.
- 00:35 And this is not a gem,
- 00:36 this is an actual little program that sits here in our Linux command line terminal.
- 00:41 So, we need to install it a little bit different than a gem.
- 00:44 If you scroll down here, here's the command we need to issue.
- 00:47 Copy this, but check it out.
- 00:48 To install, it uses this program called apt-get, and that's a Linux thing.
- 00:53 So before we issue this command,
- 00:55 we need to update our apt-get function or apt-get program.
- 00:59 So to do that type sudo apt-get update, and
- 01:02 what this does is it heads over to a repository and downloads all the updates.
- 01:08 And while that's doing that, we can copy this line,
- 01:13 come back here and paste it in, and that should install Imagemagick.
- 01:18 Now we can run our bundle install command.
- 01:21 And while it's doing that let's look at the rest of the requirements for
- 01:25 installation.
- 01:26 So look at this Quick Start section, we've got to do a few things.
- 01:29 They're all pretty easy but it's gonna take a little while.
- 01:32 This'll probably take us a couple of videos to work through all this.
- 01:36 So it's telling us we need to add something to our model and
- 01:39 that something is these two lines of codes.
- 01:42 So let's think about this for a minute, what we're wanting to do is add
- 01:45 the ability to upload images to our Pins table, our Pins model.
- 01:48 So back here when it says to make a change to our model,
- 01:52 we need to made a change to our Pin model.
- 01:54 Right here it's listed as User model but we're going to be using our Pin model.
- 01:58 So to do that come back to our app, models, pin.rb file, and
- 02:02 this is where we did that association thing a couple of videos ago,
- 02:07 we'll just tab down and paste that in.
- 02:10 And we need to make a couple of changes.
- 02:11 This avatar, that's the default, we're not gonna be using avatars,
- 02:14 we're gonna be using images.
- 02:16 So let's call this image and do the same thing down here on the second line,
- 02:21 image, and next, this bit of code right here at the end, we can delete.
- 02:25 What this is, is if an image is missing, it'll show this missing file, and
- 02:30 this is the directory where that file sits.
- 02:33 We don't need to do that for our app, so I'm just gonna delete that.
- 02:36 Go ahead and save that file, close it, head back over here.
- 02:39 Next, it says we need to run a migration.
- 02:41 And here, here's the migration it'll create but
- 02:44 we can use rails generator like we have in the past.
- 02:46 But before we run this, take a look at it, it's rails generate paperclip and
- 02:50 then user avatar.
- 02:51 User is the model and we're not gonna be using our user model, like I said,
- 02:55 we're using our pin model, so we need to change that.
- 02:57 And here again it says avatar, we're gonna change that to image.
- 03:01 So I'm gonna copy half of this and come back here and paste it into the terminal.
- 03:07 And like I said, we need to have it work on the pin and call it image.
- 03:12 So it seems that created our migration.
- 03:15 And if we look at this migration, it's the same one basically right there.
- 03:20 It's just generated it for us.
- 03:22 And like every other migration, we need to run the rake db:migrate command.
- 03:27 Do the same thing at Heroku, heroku run rake db:migrate.
- 03:32 And while that's doing that, let's look back at this stuff here.
- 03:37 We've run our migration,
- 03:40 now we need to make a change to our views and our controller.
- 03:45 We'll do that in the next video, and then we'll be done!
Lesson notes are only available for subscribers.