I’m going to teach you how to create a basic rails application STEP BY STEP. There will be images and code for each step as well. I hope this works for you guys.
I know there are no tests written, but this is just for my students to create a ‘scaffold’ of controller/models.
There are a few dependencies as well:
rvm
Sublime text
Sublime text alias (sub) that will open your project folder
In this first step, I want to create myself a new project folder. I am going to call this project “contact_list” as that is what we will be creating together, a 21st century Rolodex!
This next will will set up dependencies. Anyone who has been in a ruby project for long enough will know that RVM is huge. We will be using that in this project. To make gemset management easier, we will be creating a “.rvmrc” file.
12
% touch .rvmrc
% sub .
Inside of that “.rvmrc” file, we need to create our specific gemset. This command will force your computer to create the gemset if you haven’t yet.
1
rvm2.1@contact_list--create
This command should reload your current directory. The following screenshot will display what output you SHOULD get. If you do not get it, you need to cd out of your directory, then CD back in.
1
%cd .
Now, we are going to create our Rails project. To start, we need to install our Rails gem. After our gem is installed, we create a new project inside of our current directory with ‘rails new .’.
Now, we’re going to start our server to make sure everything is kosher. Let’s start it and check it out!
1
% rails server
We have a rails project. This is the screen you guys should be getting. This is the “Welcome Aboard” message you get when you start every project.
I want to get rid of the “Welcome Aboard” screen and start developing my own application, so I need to go into “config/routes.rb” and set up a root route. This will be where my application starts. For instance, if someone comes to my application at “www.nickscontactlist.com”, this root route will be how it starts. The following code is telling it to go to a “Home” Controller’s “Index” action.
After this, I refresh my page to see what it tells me. Remember, you need to code a little then test the output.
Awesome! This is the error I wanted. I need to initialize a Home Controller now! Remember, Controller’s are generally pluralized! This one will be different. This is my landing page, hence “Home”
I now need to create an index template. The template name matches the action name. Notice I am using the application to drive how I create things. Way cool.
1
touch app/views/home/index.html.erb
Let’s put something inside of the file as well.
1
<h1>Yey</h1>
Cool! Now, let’s create some more routes so I can do more things.
% rake routes
Prefix Verb URI Pattern Controller#Action root GET / home#index contacts GET /contacts(.:format) contacts#index POST /contacts(.:format) contacts#create new_contact GET /contacts/new(.:format) contacts#newedit_contact GET /contacts/:id/edit(.:format) contacts#edit contact GET /contacts/:id(.:format) contacts#show PATCH /contacts/:id(.:format) contacts#update PUT /contacts/:id(.:format) contacts#update DELETE /contacts/:id(.:format) contacts#destro
Let’s throw some more content in index.html.erb. I want to be able to create new data now, so let’s throw a “New Contact” link in there as well.
% rake routes
Prefix Verb URI Pattern Controller#Action root GET / home#index contacts GET /contacts(.:format) contacts#index POST /contacts(.:format) contacts#create new_contact GET /contacts/new(.:format) contacts#newedit_contact GET /contacts/:id/edit(.:format) contacts#edit contact GET /contacts/:id(.:format) contacts#show PATCH /contacts/:id(.:format) contacts#update PUT /contacts/:id(.:format) contacts#update DELETE /contacts/:id(.:format) contacts#destro
Let’s make an “Edit” link next for each contact with the route we found above.
% rake routes
Prefix Verb URI Pattern Controller#Action root GET / home#index contacts GET /contacts(.:format) contacts#index POST /contacts(.:format) contacts#create new_contact GET /contacts/new(.:format) contacts#newedit_contact GET /contacts/:id/edit(.:format) contacts#edit contact GET /contacts/:id(.:format) contacts#show PATCH /contacts/:id(.:format) contacts#update PUT /contacts/:id(.:format) contacts#update DELETE /contacts/:id(.:format) contacts#destro
Let’s have a link that shows each individual contact now:
% rake routes
Prefix Verb URI Pattern Controller#Action root GET / home#index contacts GET /contacts(.:format) contacts#index POST /contacts(.:format) contacts#create new_contact GET /contacts/new(.:format) contacts#newedit_contact GET /contacts/:id/edit(.:format) contacts#edit contact GET /contacts/:id(.:format) contacts#show PATCH /contacts/:id(.:format) contacts#update PUT /contacts/:id(.:format) contacts#update DELETE /contacts/:id(.:format) contacts#destro