Our First Resource6:38 with Jay McGavren
That welcome page is cool, but it's time to create some pages that are specific to your app. And we're going to do it using JUST TWO terminal commands. You're about to witness the power of convention over configuration.
A "resource" is a thing that you want users to be able to perform "CRUD" operations on: to create instances of it, read data for it, update data for it, and delete it when they don't want it any more.
A Rails "scaffold" creates a model, view, and controller for a resource, all at once.
In your terminal:
bin/rails generate scaffold ModelName attribute1:type attribute2:type ...
- "rails" runs the Rails command
- "generate" is the Rails subcommand we want to run. We ran the "server" subcommand before, and there are various other subcommands to do various other tasks. The "generate" subcommand will generate some source code files for us.
- "scaffold" is the type of code we want to generate. We'll look at other things we can generate later.
- "ModelName" is the model we want to create a scaffold for.
- Then, we add one or more attributes that we want our model objects to have. These are bits of data that describe the object.
Among the files created will be one with a name like "db/migrate/[date]_create_model_name.rb". This is a "migration file". It's responsible for creating the database table, which will store data for your objects.
If you ever get a "ActiveRecord::PendingMigrationError", in the terminal, type "bin/rails db:migrate". Your migration files will be run, and will set the database up for you.
To see new resources you've created a scaffold for, go to your browser's address bar and add "/model_name" at the end of the URL.
That welcome page is cool, but it's time to create some pages that are specific to 0:00 your app, and we're going to do it using just two terminal commands. 0:04 You're about to witness the power of convention over configuration. 0:08 We're going to create a Rails resource. 0:12 A resource is a type of object that you want users to be able to 0:14 create instances of, read data for, update data for, and 0:17 delete instances of when they don't want them anymore. 0:22 Creating, reading, updating and 0:25 deleting a resource are often referred to as CRUD operations. 0:27 Since we're creating a blog, 0:31 that means our app's most important resource will be posts. 0:32 We need to be able to create new posts, read existing post data like titles and 0:36 content, update or change that data, and delete posts we don't want. 0:41 Rails uses components called a model, a view and 0:45 a controller to perform these CRUD operations. 0:48 We'll go into each of those components in detail in a moment. 0:51 But for now we're going to cheat a bit and 0:54 create all three at once using a Rails scaffold. 0:56 Once you've been doing this a while, you probably won't use scaffolds much. 0:59 Because you'll want extra control over the model, view and controller you create. 1:02 But scaffolds are great when you're just getting started, 1:07 because they set everything up for you. 1:09 So let's create our first scaffold now, 1:11 and then I'll explain the components that get created. 1:13 In your terminal, check to make sure you're at your command prompt, that is, 1:17 make sure there's no Rails server running. 1:21 And then type bin/rails generate scaffold, 1:22 Post title:string. 1:31 Rails here is the command we're going to run. 1:36 Generate is the Rails subcommand we want to use. 1:38 We ran the server subcommand before, and 1:41 there are various other subcommands to do various other tasks. 1:43 Generate subcommand will generate some source code files for us. 1:46 Scaffold is the type of code we want to generate. 1:51 We'll look at other things we can generate later. 1:54 Post is the model we want to create a scaffold for. 1:56 After this runs, we'll be able to create Ruby objects with a class of post, 1:59 save them to the database and load them in again and display them in our browser. 2:03 Then we add one or more attributes that we want our post objects to have. 2:08 These are bits of data that describe the object. 2:12 We're going to specify just one attribute right now, the post title. 2:14 So that we create the right type of database field, 2:18 we need to specify the type of data the attribute will hold. 2:21 This could be an integer, a date, or any type of value the database supports. 2:24 Since the title will just be a short string, we give it a type of string. 2:29 We press Enter and the command runs. 2:33 This creates a whole bunch of files for you. 2:35 We'll come back and look at the other files in a minute, but 2:38 there's just one file I want to show you right now. 2:40 db/migrate/ today's date _create_post.rb. 2:42 This is a migration file. 2:48 It's responsible for creating the post database table, which will store data for 2:49 your post objects. 2:53 We'll run the migration to create the table here on your development system, and 2:55 it'll be run again later on your production system to create the same 2:59 table there. 3:02 But that table hasn't been created yet. 3:03 I'll show you what happens if I run bin/rails server and 3:05 reload my browser right now. 3:11 I'll see an error, an ActiveRecord PendingMigration error. 3:14 So I'll go back to my terminal and press Ctrl+C. 3:18 To fix this, we need to run the migration to create the table. 3:23 In the terminal, type bin/rails db:migrate. 3:26 Prior to Rails 5, you would have typed bin/rake db:migrate. 3:32 But the rake functionality has been merged into the rails command in Rails 5. 3:38 So bin/rails db:migrate, and hit Enter to run. 3:44 You'll see CreatePosts: migrating, in the output. 3:50 That's our migration running. 3:54 Below that you'll see in create_table(:posts). 3:56 That's the table being created. 3:59 When the migration finishes, it'll go back to our system prompt. 4:01 If we were to create a visual representation of the table that 4:05 the migration created, it would look something like this. 4:08 Right now the table has a series of empty columns, 4:11 which we'll fill in with data later. 4:13 The table has an automatically generated id column that's used to look records up, 4:15 as well as a column for each of the attributes we specified in the migration. 4:20 In this case, that's just a title column. 4:24 Migrations for new tables have a call to the timestamps method by default, 4:27 which sets up created_at and updated_at columns, which store the date and 4:31 time a record was created or updated. 4:36 Now, run bin/rails server, again, and reload your browser. 4:38 You'll see that it loads successfully this time. 4:48 It's just showing us the default welcome page though. 4:51 We want to see our post resource. 4:53 To get there, we need to specify a path to the resource we want to see. 4:55 So go to your browser's address bar and add /posts at the end of the URL. 5:00 We'll see a page with a list of posts, but there aren't any yet. 5:07 Let's try a Create operation. 5:10 Click the New Post link and you'll see a form to create a post. 5:13 Only the title field exists right now. 5:16 So fill that in and click the Create Post button. 5:18 You'll see the message, Post was successfully created. 5:25 You can click the back link to get back to the list, and 5:28 you can add another post if you want. 5:30 For a read operation, you can click the Show link next to any post in the list, 5:39 read the existing post data. 5:44 For an update operation, click the Edit link next to any post. 5:47 You'll be presented with a form just like the one for creating new posts, 5:52 except that it's pretty populated with the existing post data, And 5:55 it updates the existing data when you click the button. 6:01 Lastly, for a delete operation, click the Destroy link next to any post. 6:04 You'll see a confirmation dialog, and if you click OK, 6:09 the post will be deleted from the list. 6:12 So, now we're able to create, read, update and delete instances of our first resource 6:17 from our browser, and we only had to type a couple rails commands to do it. 6:21 But as you might imagine, 6:26 a whole lot is going on behind the scenes to make this happen. 6:27 And before you can customize this interaction, 6:30 you're gonna need to understand what's going on. 6:33 We'll take a look at what's happening next. 6:35
You need to sign up for Treehouse in order to download course files.Sign up