Generating the Scaffold in Ruby on Rails8:57 with Jim Hoskins
In this video, we generate the scaffold for our Jobs in our application. We Install generators to create the view scaffolds in haml, so Nick will be able to work on the views in HAML right away.
[Master Class] [Designer and Developer Workflow] [Generating the Scaffold] 0:00 So, up to this point, we've generated a new Rails project. 0:05 We haven't really done anything with it, but we've created the skeleton for it, 0:07 and we've set up our GitHub account and sent the details over to Nick. 0:11 So hopefully by now, he can at least pull the project and set up the basic Rails stuff. 0:16 And our next step is to go ahead and configure it and see if we can get it up and running. 0:21 Now, I just got a message from Nick. 0:26 Let's go ahead and take a look. 0:28 "Hey Jim, I was wireframing the show view for the jobs...and I was thinking 0:31 that there were a few fields that we could add." 0:36 "I know we originally agreed on just having a job title and description for the job." 0:39 "But I think it would also be nice to add the name of the company 0:43 and a details link in case the company wants to include an external link 0:47 so they can see more details about the job." 0:52 Okay, so it looks like we've gone from having our job just be a title and description 0:56 to now having a company name and a details link. 1:04 That looks like a pretty easy change, but I'm glad Nick went ahead and sent that 1:10 as soon as he did so I don't have to change an existing model 1:13 because I was just about to do that, so the timing is perfect. 1:16 So, let's add that. 1:20 All right, so let me just go ahead and reply to him. 1:22 "Hey Nick, that all sounds pretty good." 1:25 "I'll go ahead and add those, and I'll let you know exactly what the different fields are 1:27 when I go ahead and generate it." 1:31 "Give me a few minutes and I'll go ahead and push the scaffold up with these details." 1:34 So now, let's go ahead and do what we promised to do. 1:40 Before we can scaffold anything, let's go ahead and see if our Rails application is even running. 1:43 So, the first thing I want to do is run bundle install. 1:49 So now, we've run bundle and it's gone ahead and installed all the dependencies 2:09 that are needed to run this application. 2:12 Let's start up our server and see if it works. 2:15 So, we'll type in "rails server" or just "rails s," and let's go over to our browser. 2:17 And if we load up local host 3000, we can see the Rails welcome screen. 2:28 So, it looks like our server is up and running. 2:32 Let's go ahead and generate our first scaffold. 2:35 So, let's go ahead and stop the server, and we will be generating a new scaffold. 2:40 If you want to learn more about scaffolds, go ahead and watch our videos on Ruby on Rails, 2:45 so I'm just going to be going through this pretty quickly. 2:49 We'll do "rails g" for Rails generate, and we're going to create a scaffold. 2:51 And our model is going to be called a job. 2:56 And if we look at our information, we need a title, description, 3:01 company name and details link. 3:05 Our title is going to be "string," our description is going to be "text," 3:10 our company name will be "string," and the details link will be another string. 3:18 A pretty simple model. 3:28 So, let's see what happens when we install ithis. 3:30 All right, that looks pretty good. 3:35 However, I did notice something. 3:37 We did generate html.erb files. 3:39 Now, Nick and I decided we wanted to work on HAML and SASS, 3:42 so let's go ahead and destroy all this, install HAML and SASS, 3:45 and see if we can get the correct generators to work. 3:49 So, we'll type in "rails destroy scaffold job." 3:55 And that's gone and undone everything that the generator did. 4:05 So, let's go ahead and install HAML. 4:08 So, let's open up our gem file. 4:12 And we're going to go ahead and add the gem HAML as a dependency. 4:18 So, we'll add "gem haml" and we'll save that out. 4:24 So, let's go back to our terminal and type in "bundle install" 4:29 so we can install HAML. 4:32 So now, we've installed so our Rails application will now be able to handle .haml files. 4:39 Let's go ahead and run our generator again and see what happens. 4:46 Now, we're still getting our index.html.erb, and that's because installing HAML 4:52 only allows us to read HAML files, it doesn't install any generators 4:56 for our scaffold to generate in HAML. 5:03 Now, we could go in and rename the ERB file to HAML and recreate them all ourselves, 5:07 but it'd be nice if our scaffold did come with HAML by default. 5:11 Now, one tool we can do to have Rails generate HAML is use the gem called "HAML Rails." 5:16 And this will add the generators that will create our different scaffold views 5:21 in HAML instead of .erb files. 5:25 So, we'll go back, add HAML Rails to our gem file, 5:29 save it out, and run bundle install again. 5:37 So now, we have HAML Rails installed. 5:46 Let's go ahead and destroy our original scaffold again, 5:51 and then let's go ahead and rerun our scaffold generator and see what we get. 5:58 It looks like it generated the HAML views, which looks pretty good. 6:05 So, if we go into our text editor, let's take a look. 6:09 So, we have our jobs, looks like our index.html.haml. 6:17 Looks like HAML to me and that'll be a good starting point for Nick to work off of. 6:21 And so, I think we're good to go ahead and stick with this generator. 6:27 So, let's go ahead and set up our database. 6:32 Now, by default, we're using SQLite3, so we'll run the task 6:34 "rake db: create" to go ahead and create our database. 6:37 Done, and our generator set up a migration for us for our jobs, 6:43 so we'll run "rake db: migrate." 6:47 And it's gone ahead and created the table "jobs." 6:54 So, if we start up our server, we can see where we're at. 6:57 So, going back here to local host 3000, the root page is still going to be the welcoming page. 7:06 And let's go to /jobs. 7:12 All right, we got our normal listing here. 7:16 We can create a new job, say "Ice Cream Taster." 7:18 Add some text and we'll give it a company name, and we'll give it a details link. 7:25 Save it out. 7:34 We have our show page for jobs, and we have our listing here. 7:36 And this is enough to send over to Nick where he'll begin working on the layout, 7:41 the user interface, the user experience and all of that 7:45 to actually make the app run properly. 7:48 So, I think this is a good point for a commit, so let's go ahead and do just that. 7:50 We'll stop the server. 7:56 We'll do "git add ." 7:58 We'll do "git commit," and we'll say we "Generated the jobs model." 8:01 And I'm going to go ahead and "push origin master." 8:15 And now it is up on GitHub. 8:26 So now, I'm going to go ahead and send a message to Nick telling him about this, 8:29 and letting him know he can start working on the views. 8:31 "Hey Nick, I went ahead and ran the generator for our jobs, so if you pull now 8:35 you'll have the jobs scaffold at /jobs." 8:39 "I named the fields: title, description, company name and details_link, 8:43 and of course you have the created_at and updated_at fields for the dates." 8:50
You need to sign up for Treehouse in order to download course files.Sign up