Thursday, December 6, 2012

Guide to start a Rails application

Most of the time that I create a Rails application I follow the same steps. This guide will help me (maybe to you too) to create the next application.
#Create the application
rails new DEMO
#Execute the server
rails s
#Open in a browser localhost:3000
cd DEMO
#If I'm in Mac OS
mate .
#If I'm in Linux or Windows I use SublimeText2
#subl .
#Create a controller to be the front page of the application
rails g controller welcome index
#Delete the index.html page, if not, the server will continue redirecting
rm public/index.html
Open the file /config/routes.rb and uncomment this line. This will route all the access to / (root) to the index page of the welcome controller.
root :to => 'welcome#index'
It is recommended to have a version control and for this I'll use Github. Go to github.com and create a repository with the name of your application (DEMO). Do not create a README neither check any box.
#Create the README file with the description of your project.
echo "Description of your application" > README.md
#Initialize the git project
git init
#Add everything and commit, this will not sync your repository.
git add .
git commit -m "First commit"
#You may get this line from github when you create the repository.
#Replace my username with yours.
git remote add origin git@github.com:johandry/DEMO.git
#Synchronize or, in this case, send everything to your repo
git push origin master
Create the first Scaffold
rails generate scaffold Host name:string domain:string ip:string description:text image_url:string
rake db:migrate
Open app/views/welcome/index.html.erb and replace the content by:

Open db/seed.rb and add all the information you want to insert in your database. This is useful when you need a lot of data in your database to play with your application.
Host.delete_all

Host.create(
 name: 'host01',
 domain: '.com',
 image_url: 'host01.jpg'
)

Host.create(
 name: 'host02',
 domain: '.com',
 image_url: 'host01.jpg'
)
Execute this to populate your database. Notice that this will delete the database first, so make sure you have not write nothing important.
rake db:seed
Go to localhost:3000 and check what is done.

Commit the changes with...
git add .
git commit -m "Host scaffold created"
git push origin master
Let's make a pretty page now. Open app/assets/stylesheets/hosts.css.scss and add this code:
.hosts {
 table {
   border-collapse: collapse;
 }

 table tr td {
  padding: 5px;
  vertical-align: top;
 }

 .list_image {
  width: 40px;
 }

 .list_description {
  width: 80%;

  dl {
   margin: 0;
  }

  dt {
   color: #244;
   font-weight: bold;
  }

  dd {
   margin: 0;
  }
 }

 .list_actions {
  font-size: x-small;
  text-align: right;
  padding-left: 1em;
 }

 .list_line_even {
  background: #ededed;
 }

 .list_line_odd {
  background: #ffffff;
 }
}
Open the file app/views/layouts/application.html.erb and replace the <body> tag by:

Open list view of Host (app/views/hosts/index.html.erb) and replace the content with:

Copy a 2 images in the directory app/assets/images named host01.jpg and host02.jpg

Commit the changes with...
git add .
git commit -m "Style changes to Host listing"
git push origin master
Next steps: Validations, unit tests, add and associate models

Source and external guides:

Post a Comment