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