Rails For Newbies

Starting with Rails

created by: Ingrid G.

Recommendation to understand a little ruby on rails language watch the next video:
its Free!
http://www.codeschool.com/courses/rails-for-zombies

Requirements

*RVM 1.9.2+
*Ruby 1.9.2+
*Rails 3.2.3 +
*A text editor (the one you like best, Sublime, TextMate etc.)
*Postgres database.

Activity

This is a small application to keep track of "Tanking Controls" that users make to their cars.
-A user must be registered
-A user must be able to enter
-A user should be able to create, edit, view and delete their cars 
-A user must be able to register " Tanking "
-In the same form where the user registers the "Tanking", be able to register a new station if within is not shown that he wants.
-The station only has the field: name.
-You should see some statistical reports with graphics, where you specify:
  + Station with gasoline more expensive
  + Station where gasoline yields more
  + Historic of "Tanking" by car

Fields

Car
brand String
model String
color String
year Int

User
Name String
Email String
Password String
Password Confirm String

Tanking Logs
Cost Float  
Gallon Float
Km Int
Date date
Car_id Int
Gas_station_id Int

Gas Station
name String  

Note:
Rails automatically creates the Id field
For the login and user management use Devise gem
For the design of the page twitter-bootstrap gem and statistical graphs: jquery google chart or plot.
you have to put the project on github and Heroku.

Creating the project

go to the folder where you want to keep your projects

note: with the command "cd " open the folder on terminal , with "cd .. " go back and "ls" show the current project.

create a new project on rails

rails new ProjectName
rails new Station

go to the project folder on command prompt, in this case Station and open the Project with your text editor.

Edit your gems

gems you need for the project

search the file , "Gemfile" ,delete first the gem default of your project , copy and paste this

gem 'rails', '3.2.3'
gem 'pg', '0.13.2'
gem 'devise', '2.0.4'
gem 'haml', '3.1.4'
gem 'twitter-bootstrap-rails', '2.0.6'
gem 'sass-rails',   '~> 3.2.3'

group :assets do
  gem 'coffee-rails', '~> 3.2.1'
  gem 'uglifier', '>= 1.0.3'
end

gem 'jquery-rails',"2.0.2"
gem 'jquery-ui-rails',"0.3.0"

gems will enable you to use other tools that you require in your project, either for the design of your website as a bootstrap, for the authentication using devise , establish the connection to the database in this case postgress among others , to see other gems and description can go to http://rubygems.org/

go to the command promp and type

bundle install

(you need to stay on the project folder that rails generated it)

bundle install all you gems that you have on your Gemfile.

Config Database

go to projectname/config/database.yml (text editor)

rails generate a default connection with SQLite ,but we have to change for Postgres database.

development:
  adapter: postgresql
  database: station_development
  host: localhost
  username: koombea
  pool: 5
  timeout: 5000

test:
  adapter: postgresql
  database: station_test
  host: localhost
  username: koombea
  pool: 5
  timeout: 5000

production:
  adapter: postgresql
  database: station_test_production
  host: localhost
  username: koombea
  pool: 5
  timeout: 5000

note : use your username that you created on postgres in this case was koombea, and if you have password you have to put it

got to command prompt and type

rake db:create

it create the database on Postgres

Install Devise

on command prompt type 

rails g devise:install 
rails g devise User  name:string
rake db:migrate

devise install default fields email and password , but if we need to add name of the user just put the variable and the datatype , for example name:string

then create the devise views 

rails generate devise:views

you can see it on projectName/app/views/devise

Install Twitter Bootstrap

 rails g bootstrap:install

see the Documentation twitter-bootstrap-rails gem https://github.com/seyhunak/twitter-bootstrap-rails

if you want to edit your views with haml can start doing it on html then turn it. while gaining practical.
http://html2haml.heroku.com/

note: use a .haml file

Models

create Car model 

rails g model Car user_id:integer brand:string model:string year:integer color:string

note : you can simplify rails generate with rails g

the prams are brand , model, color, year and user_id 

you need to put the user_id because you have a relationship with User table.

rails g model GasStation name:string 

rails g model TankingLogs car_id:integer cost:float date:datetime gallon:float gas_station_id:integer km:integer

Relationship and Validation

See the information about Relationship on rails http://guides.rubyonrails.org/association_basics.html#the-types-of-associations

if you want to add the relationship with rails you have to put on your models

has_many and belongs_to

as the name says has many instances of this parameter and belongs_to we can instance the foreign key of the table that belongs

Lets Start with Relationship and Validation
now on your text edit go to projectName/app/models/user.rb

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :name, :email, :password, :password_confirmation, :remember_me

  has_many :cars

  validates_presence_of  :email

end

now on your text edit go to projectName/app/models/car.rb

you can see

class Car < ActiveRecord::Base

     attr_accessible :brand, :color, :model, :user_id, :year
end

you have to add the relationship with the User, and TankingLogs
class Car < ActiveRecord::Base
  belongs_to :user
  has_many :tanking_logs

  attr_accessible :brand, :color, :model, :user_id, :year

  validates_presence_of :brand, :color , :model , :year

end

note: attr_accessible you can "access" to the parameter on the table , for create , delete , update and read .
validates_presence_of: prevents that the variable can't be blank

See more validation on http://ar.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html

now on your text edit go to projectName/app/models/gas_station.rb

class GasStation < ActiveRecord::Base
  has_many :tanking_logs

  attr_accessible :name

  validates_presence_of :name
end

now on your text edit go to projectName/app/models/tanking_log.rb
class TankingLog < ActiveRecord::Base
  belongs_to :gas_station
  belongs_to :car

  attr_accessible :car_id, :cost, :date, :gallon, :gas_station_id, :km

  validates_presence_of :cost , :date , :gallon , :km
  validates_numericality_of :cost , :gallon
  validates_numericality_of :km, only_integer: true

end

note: validates_numericality you can only type numbers on the field

go to the command prompt and type

rake db:migrate

create the tables on the database

Controllers

Lets Start with Controllers

the controllers receive the request for create , read, update and delete, etc. data from the models and using the views to generate a output.

rails g controller Car

to use the crud operations ,you almost have to implement the methods

new , create , show, edit , update , destroy, index

See more about create and implement controller with the views on rails
http://guides.rubyonrails.org/action_controller_overview.html

Views

when you generate a controller on rails ,
automatically it generate a folder with the controller name , you have to create a .html.erb file with the name of the method
see more on http://guides.rubyonrails.org/layouts_and_rendering.html
remember use Twitter Bootstrap
see the documentation of bootstrap , it teach how to make the layout , it's very useful
http://twitter.github.com/bootstrap/

Github

Online project hosting , you have to use much
install git on your computer , follow the instruction

http://help.github.com/mac-set-up-git/

create an account https://github.com/

then create a repository with the name of the project

its put a text and you have to follow the instruction
if you created the project , just go to the folder on terminal and start on git init , and git add . (include the point ,for all files project)

mkdir prueba
cd prueba
git init
touch README
git add README
git commit -m 'first commit'
git remote add origin moc.buhtig|tig#moc.buhtig|tig:name/prueba.git
git push -u origin master

Heroku

create an account heroku , install it , and follow the instruction

https://toolbelt.herokuapp.com/osx

open the project create on heroku website
copy the Git Repo
now on your terminal type

git remote add heroku git@heroku.com:NameofyourApp.git

git push heroku master

heroku run rake db:create --app nameApp

heroku run rake db:migrate --app nameApp

heroku open --app nameApp

Activity 2

should do the same activity only this time using backbone.js
You can find many gems to use rails with backbone.js
here is the documentation of one https://github.com/meleyal/backbone-on-rails
read more http://documentcloud.github.com/backbone/

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License