Locked lesson.
About this lesson
Adding, removing, and installing Gems.
Exercise files
Download this lesson’s related exercise files.
Understanding the Gemfile and Gems.docx58.7 KB Understanding the Gemfile and Gems - Solution.docx
58.9 KB
Quick reference
Understanding the Gemfile and Gems
To add third party functionality to our Rails app, "Gems" are added to the "Gemfile".
When to use
Any time you want to add any sort of third party functionality to your app, you'll use a gem from RubyGems.org. Simply add it to your Gemfile and run the "bundle install" command in the terminal.
Instructions
Adding things to your app is very easy with Rails. There's an entire community of Gems available for free at RubyGems.org.
To add a Gem, paste the Gem reference into your Gemfile, then run the "bundle install" command from the terminal.
Hints & tips
- Gems can be found at RubyGems.org
- Gems are added to the Gemfile
- To Install Gems, run the "bundle install" command from the terminal after adding the gem reference to the Gemfile
- Sometimes Gems come with additional installation instructions - so always read the documentation!
- 00:05 In this video, I wanna talk a little about Gemfile.
- 00:07 And we've looked at this earlier, but this just a really important file.
- 00:11 And we're gonna be using this a whole lot throughout this course and
- 00:14 throughout your Rails coding career.
- 00:17 So you wanna be comfortable with this Gemfile.
- 00:19 And earlier we went through here and we cleaned up all the comments, so
- 00:22 it's much easier to read.
- 00:23 I touched on this earlier, but let's talk about this, what exactly is the Gemfile?
- 00:28 Well, the great thing about Ruby on Rails is it allows you to do all kinds of stuff
- 00:32 without writing code yourself.
- 00:34 You can take things that other people have created, and import them into your
- 00:38 project, and use them without having to write all of the stuff.
- 00:41 And we're gonna see this pretty soon when we start adding things.
- 00:43 And those things that we import, they're called gems.
- 00:46 It's, you know, Ruby has a gem, it's a whole theme, right?
- 00:50 So anytime we wanna add something from a third party, we're gonna add a gem.
- 00:54 And we're gonna do it to this file.
- 00:56 And every time we add something we have to run the command bundle install,
- 01:01 and what that does is it downloads everything and it installs it.
- 01:06 You can see we haven't added anything, we're complete,
- 01:09 we're up to date, so it didn't do anything.
- 01:12 But it did go through here and check.
- 01:13 Now, a lot of these things have dependencies.
- 01:16 So there's one gem here, but it might need five other gems in order to work itself.
- 01:21 The bundle install command will go and find all of the other files that you need,
- 01:24 it'll download them, and you'll be up and running, and good to go.
- 01:27 The question becomes where do you find all these gems, and how do you use them?
- 01:31 Well, there a website called rubygems.org and
- 01:35 it is huge, it is the main repository of all gems ever.
- 01:40 And people can add there own gems and other people can download and use them.
- 01:44 And you can see, 12 billion downloads and counting, this is the place to go.
- 01:49 So you noticed earlier we added Postgres which is pg.
- 01:54 You can see we added it to allow Heroku to play nice with our app.
- 01:58 Well, if I type in pg, you see here is 31 million downloads,
- 02:03 and if we click on this, see here's the Gemfile.
- 02:07 This is the line of code, you'll copy this, and then you'll come back here, and
- 02:11 you'll paste it in, Control V.
- 02:13 And it pops it in.
- 02:15 Right here I just typed in pg, but
- 02:16 you see this squiggly line on the other side of it.
- 02:19 That signifies a version number.
- 02:22 So this is version 0.19 of the pg value.
- 02:26 You don't always have to do that.
- 02:28 It's good that you do that in the future.
- 02:32 When I added pg we didn't really need a version number, I knew that already so
- 02:36 I didn't add one.
- 02:37 But in the future you will add definitely a version number to your Gemfiles.
- 02:40 Basically, just copy and paste this, and you'll see as we move forward and
- 02:44 start using the Gemfile, this becomes second nature to you.
- 02:48 But looking through here there's some interesting stuff.
- 02:50 There's usually a Homepage and a Documentation.
- 02:52 And a lot of times the Homepage and the Documentation page are the same page.
- 02:56 But if you click on here, I don't like the look of that.
- 02:58 Let's go to the Homepage.
- 03:00 You'll often find installation instructions because it's
- 03:04 not always as easy as just typing in a name into your Gemfile and
- 03:07 then running the bundle install.
- 03:09 A lot of times there's additional steps you have take and
- 03:12 the Read Me file will tell you that.
- 03:14 So, it's always important to go ahead and read through the Read Me file.
- 03:17 Another thing to mention is a lot of times there's a bunch of different listings
- 03:21 that look similar, and it can be confusing which one you need to download.
- 03:25 A good rule of thumb is you almost always want the one with the most downloads.
- 03:29 So pg has almost 32 million downloads.
- 03:33 The next one down is 1 million.
- 03:36 You can guess that this is the one we want and this is not the one we want.
- 03:40 So that's something sort of useful to keep in mind, but really simple to use.
- 03:44 That's one of the great things about Rails.
- 03:46 It allows you to add all kinds of cool stuff to your apps without
- 03:49 writing code yourself without inventing things yourself.
- 03:52 You can take gems that other people have created and use them, just drag and
- 03:56 drop, easy.
- 03:57 So you'll see as we move forward, we'll use this a lot.
- 04:00 But right now, just sorta get familiar with this file, and
- 04:04 yeah, that's all for this video.
Lesson notes are only available for subscribers.