Escolar Documentos
Profissional Documentos
Cultura Documentos
Rails
Web application framework written in ruby Based on Model View Controller architecture (MVC) Uses core APIs and generators to
reduce the coding burden and focus your efforts on value-adding development
9/7/2011
Why Rails
Productive
Programming in Ruby is simpler Boiler plate already in place (e.g. binding objects between tiers) Gems for re-usable features
Each to understand
Convention over configuration Configuration-like programming e.g. ActiveRecord
9/7/2011
Install
MySQL 5.X HeidiSQL (SQL Client) Ruby 1.8.7
http://www.ruby-lang.org/en/downloads/ Note version
Rails
gem install rails v=2.3.5
9/7/2011
Nokia Music
Project Setup
Create a database
mysql u root -p create database library; GRANT ALL ON library.* TO library@localhost IDENTIFIED BY library ; FLUSH PRIVILEGES;
9/7/2011
Nokia Music
Project Setup
Configure your application to communicate to the database
config/database.yml
development: adapter: mysql database: library username: library password: library host: localhost
9/7/2011
Nokia Music
Concepts
Rake Routes Migrations Generators Object relationships Validates
9/7/2011
Nokia Music
Rake
Rake is rubys build program (similar to make and ant) Rake uses rakefiles to manage the build
Written in ruby Default one created for rails which includes standard tasks
Rake tasks are namespaced To see all the available rake tasks run rake T or rake tasks Most commonly used rake tasks
rake db:migrate migrate the database to the current version rake db:rollback move the database back a version rake test run all tests (unit tests and functional tests)
9/7/2011
10
Routes
The rails router matches incoming requests to controller actions Routes are configured in config/routes.rb Some generators add routes e.g. ruby script/generate scaffold cd name:string artist:string genre:string
Will add routes to add, delete, update, show, list posts
Routes can also be used to generate URLs for links, forms e.g.
link_to @cd.name, cd_path(@cd) creates a link to the post show page
Migrations
Migrations allow you to manage a database through versions Each version is held in a separate timestamp prefixed file in db/migrate Each migration knows how to update the database (self.up) and how to rollback (self.down) Migrations are written in ruby and the migrations api supports a wide range of table and column alterations You can also run normal SQL and ruby code Every time a migration is run the <table> table is updated to include the timestamp Migrations with rake db:migrate and rake db:rollback
9/7/2011 12
Generators
Command line interface to run code generators Rails comes with a set of out of the box templates
You can customise these You can add your own
9/7/2011
13
Object relationships
ActiveRecord is ruby implementation of the active record pattern (Martin Fowler 2003) Set of meta-programming methods allow you to configure relationships in your model objects:
belongs_to :cd - for table holding pkey has_many :tracks - notice the plural, for connected table has_many :genres, :through => :cd_genres to link through a relationship table
Then you can call methods on your model objects e.g. @cd.tracks # array of tracks @cd.genres # array of genres @track.cd # cd model object
9/7/2011
14
Validations
Rails makes a set of validation methods available Configure them in your model object Rails validates on save and stores and saves it in <object>.errors Some examples:
validates_presence_of :title, :artist mandatory field checks validates_uniqueness_of :title each title can only be used once validates_numericality_of :quantity must be a number
9/7/2011
15
Console
Console lets you run your application through the command line and test out pieces of code To start a console session
ruby script/console
From there you can run ruby commands and will have access to all of your objects The console saves a lot of time loading and reloading web pages to test functionality Some useful commands
_ - provides access to the last result e.g. @cd = _ puts @cd.to_yaml (or y @cd) writes out an indented version of the object reload! reload the app <tab> - autocompletes methods
9/7/2011 16
9/7/2011
18
9/7/2011
19
Caching Rails.cache.read/write/delete and config for cache setup Deployment with capistrano Ajax via jQuery and format.js
9/7/2011
21
9/7/2011
23