RailsBeginners Guide
Rails• Web application framework written in ruby• Based on Model View Controller architecture (MVC)• Uses core APIs and ge...
Why Rails• Productive    – Programming in Ruby is simpler    – Boiler plate already in place (e.g. binding objects between...
SetupGetting your environment ready
Install• MySQL 5.X• HeidiSQL (SQL Client)• Ruby 1.8.7    – http://www.ruby-lang.org/en/downloads/    – Note version• Rails...
Project Setup• Create a database    – mysql –u root -p    – create database library;    – GRANT ALL ON library.* TO ‘libra...
Project Setup• Configure your application to communicate to the  database    – config/database.yml        • development:  ...
ConceptsHigh-level Overview
Concepts•   Rake•   Routes•   Migrations•   Generators•   Object relationships•   Validates6/16/2011 Nokia Music      9
Rake• Rake is ruby’s build program (similar to make and ant)• Rake uses rakefiles to manage the build    – Written in ruby...
Routes• The rails router matches incoming requests to  controller actions• Routes are configured in config/routes.rb• Some...
Migrations• Migrations allow you to manage a database through  versions• Each version is held in a separate timestamp pref...
Generators• Command line interface to run code generators• Rails comes with a set of out of the box templates    – You can...
Object relationships• ActiveRecord is ruby implementation of the active  record pattern (Martin Fowler 2003)• Set of meta-...
Validations• Rails makes a set of validation methods available• Configure them in your model object• Rails validates on sa...
Console• Console lets you run your application through the  command line and test out pieces of code• To start a console s...
Simple ProjectA CD library
Create your model and scaffold• rails library• cd library• ruby script/generate scaffold cd name:string  artist:string gen...
Create relationships between models• ruby script/generate scaffold track name:string  cd_id:integer• rake db:migrate• Crea...
NextWhere to go next
Taking a step beyond the basics• Access other people’s shared code via gems and plugins    – authlogic – controllers and U...
ReferencesUseful links
Further reading and videos• Railscasts – http://www.railscasts.com    – Video tutorials from Ryan Bates• Pivotal Labs - ht...
Upcoming SlideShare
Loading in …5
×

Rails - getting started

783 views
728 views

Published on

A getting started guide to rails version 2.3.5

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
783
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Rails - getting started

  1. 1. RailsBeginners Guide
  2. 2. 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• Large community of developers – Provide support to each other – Write gems (libraries) to extend the framework (see rubygems.org)6/16/2011 2
  3. 3. 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• Large community• Matur(e/ing)6/16/2011 3
  4. 4. SetupGetting your environment ready
  5. 5. 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.56/16/2011 Nokia Music 5
  6. 6. Project Setup• Create a database – mysql –u root -p – create database library; – GRANT ALL ON library.* TO ‘library’@’localhost’ IDENTIFIED BY ‘library ‘; – FLUSH PRIVILEGES;• Create a rails application – rails library6/16/2011 Nokia Music 6
  7. 7. Project Setup• Configure your application to communicate to the database – config/database.yml • development: • adapter: mysql • database: library • username: library • password: library • host: localhost6/16/2011 Nokia Music 7
  8. 8. ConceptsHigh-level Overview
  9. 9. Concepts• Rake• Routes• Migrations• Generators• Object relationships• Validates6/16/2011 Nokia Music 9
  10. 10. Rake• Rake is ruby’s 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)6/16/2011 10
  11. 11. 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• The routes API supports a multitude of operations, a common ones is: – map.resources :cds – creates CRUDL routes6/16/2011 11
  12. 12. 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:rollback6/16/2011 12
  13. 13. 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• Typically generate classes into your app directory e.g. – ruby script/generate model cd name:string artist:string genre:string – generates a model and associated files (tests, migrations) with the attributes specified – ruby script/generate scaffold cd name:string artist:string genre:string - creates not only the model but also a CRUDL controller and views – ruby script/generate migration add_record_label_to_cd – creates a single migration6/16/2011 13
  14. 14. 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 object6/16/2011 14
  15. 15. 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 number6/16/2011 15
  16. 16. 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 methods6/16/2011 16
  17. 17. Simple ProjectA CD library
  18. 18. Create your model and scaffold• rails library• cd library• ruby script/generate scaffold cd name:string artist:string genre:string• rake db:migrate• ruby script/server• http://localhost:3000/cds6/16/2011 18
  19. 19. Create relationships between models• ruby script/generate scaffold track name:string cd_id:integer• rake db:migrate• Create associations in the model classes – track.rb - belongs_to :cd – cd.rb - has_many :track• Allow tracks to select an album in track/edit.html.erb – <%= select( “track", “cd_id", Cd.all.map {|cd| [cd.name, cd.id]}, { :include_blank => "No part of an album" }, {:class => "fieldSelect"}) %>6/16/2011 19
  20. 20. NextWhere to go next
  21. 21. Taking a step beyond the basics• Access other people’s shared code via gems and plugins – authlogic – controllers and UI to enable authentication – will_paginate – rich pagination for lists – cucumber – behaviour driven testing – faker – data generator – paperclip – file attachments• Caching – Rails.cache.read/write/delete and config for cache setup• Deployment with capistrano• Ajax via jQuery and format.js6/16/2011 21
  22. 22. ReferencesUseful links
  23. 23. Further reading and videos• Railscasts – http://www.railscasts.com – Video tutorials from Ryan Bates• Pivotal Labs - http://pivotallabs.com/talks – Wide range of talks including rails from leading tech company• has_many :through blog - http://blog.hasmanythrough.com/ – John Susser’s blog, senior rails developer at Pivotal Labs• Ruby Doc - http://www.ruby-doc.org/ – Ruby class documentation6/16/2011 23

×