Ruby On Rails (RoR), is an open sourrce web application framework for the Ruby Programming Language. It is intended to be used with an Agile development methodology that is used by web developers for rapid development. Rails debuted in late 2004 and changed the way a lot of people think about web development.
Rails used the Model-View-Controller (MVC) architecture pattern to organize application programming.
Today in 2010, we’re looking at a totally new, more mature framework in Rails 3.0 .
Here in the article , it is focused on the features Rails 3 . Rails 3 is a huge release that includes the combined power of Rails and Merb. Merb (Mongrel+Erb), is a model-view-controller web framework written in Ruby.
1: Upgrading to Rails 3
Rails 3.0 requires Ruby 1.8.7 or higher. Support for all of the previous Ruby versions has been dropped officially . Rails 3.0 is also compatible with Ruby 1.9.2.
To Install
[text]
gem install tzinfo builder memcache-client rack rack-test rack-mount erubis mail text-format thor bundler i18n
gem install rails –pre
[/text]
Notes: The first line is required because RubyGems currently can’t mix prerelease and regular release gems
2: Creating a Rails 3.0 application
[ruby]
$ rails new myapp
$ cd myapp
[/ruby]
3: script/* replaced by script/rails
The new script/rails replaces all the scripts that used to be in the script directory. You do not run script/rails directly though, the rails command detects it is being invoked in the root of a Rails application and runs the script for you. Intended usage is:
Rails 2 | Rails 3 |
script/generate | rails g |
script/console | rails c |
script/server | rails s |
script/dbconsole | rails db |
4: New Routing API
4.1 Improved DSL
While the old map.connect DSL still works just fine, the new standard DSL is less verbose and more readable.
[ruby]
# old way
ActionController::Routing::Routes.draw do |map|
map.connect "/main/:id", :controller => "main", :action => "home"
end
# new way
Basecamp::Application.routes do
match "/main/:id", :to => "main#home"
end
[/ruby]
First, the routes are attached to your application, which is now its own object and used throughout Railties. Second, we no longer need map, and the new DSL (match/to) is more expressive. Finally, we have a shortcut for controller/action pairs (“main#home” is {:controller => “main”, :action => “home”).
Another useful shortcut allows you to specify the method more simply than before:
[ruby]
Basecamp::Application.routes do
post "/main/:id", :to => "main#home", :as => :homepage
end
[/ruby]
4.2 Comparison between Rails2 and Rails3
Simple routes
Rails 2
[ruby]
map.resources :posts,:member={:confirm=>:post,:notify=>:post} do |post|
post.resources :comments,:member=>{:preview=> :post},:collection=>{:archived=>:get}
end
[/ruby]
Rails 3
[ruby]
Resources :posts do
member do
post :confirm
get :notify
end
Resources :comments do
post :preview, :on=>:member
get :archived, :on=>:collection
end
end
[/ruby]
Root mapping
Rails 2
[ruby]
map.root :controller=>”user”
[/ruby]
Rails 3
[ruby]root :to=>”user#index”[/ruby]
Legacy Route
Rails 2
[ruby]
Map.connect ‘:controller/:action/:id’
Map.connect ‘:controller/:action/:id.:format’
[/ruby]
Rails 3
[ruby]
Match ‘:controller(/:action(/:id(/:format)))
[/ruby]
5. Action Mailer
Action Mailer has been given a new API with TMail being replaced out with the new mail as the Email library. Action Mailer itself has been given an almost complete re-write with pretty much every line of code touched. The result is that Action Mailer now simply inherits from Abstract Controller and wraps the Mail gem in a Rails DSL.This reduces the amount of code and duplication of other libraries in Action Mailer considerably.
1. Can now send email using new API with three methods: attachments, headers and mail.
2. ActionMailer now has native support for inline attachments using the attachment inline method
3. Action Mailer emailing methods now return Mail::Message objects, which can then be sent the deliver message to send itself.
4. All delivery methods are now abstracted out to the Mail gem.
5. The mail delivery method can accept a hash of all valid mail header fields with their value pair.
6. The mail delivery method acts in a similar way to Action Controller’s respond_to, and you can explicitly or implicitly render templates. Action Mailer will turn the email into a multipart email as needed.
7. You can pass a proc to the format.mime_type calls within the mail block and explicitly render specific types of text, or add layouts or different templates. The render call inside the proc is from Abstract Controller and supports the same options.
8. What were mailer unit tests have been moved to functional tests.
9. Action Mailer now delegates all auto encoding of header fields and bodies to Mail Gem
10. Action Mailer will auto encode email bodies and headers for you
11. All mailers are now in app/mailers by default.
In Rails 2 we use
[ruby]
$ script/generate mailer UserMailer welcome forgot_password
this create app/model/user_mailer.rb
def welcome(user,subdomain)
subject ‘Welcome To Mail test Page”
recipients user.email’
from ‘admin@testapp.com’
body :user=>user,:subdomain=>subdomain
end
[/ruby]
In Rails 3
[ruby]
$ r g mailer UserMailer welcome forgot_password
this create app/mailer/user_mailer.rb
def welcome(user,subdomain)
@user=user
@subdomain=subdomain
mail(:from=>”admin@testapp.com”,
:to=>user.email,
:subject=>” admin@testapp.com”)
end
[/ruby]
6: Active Record Query Interface
Active Record, through the use of Arel, now returns relations on its core methods. The existing API in Rails 2.3.x is still supported and will not be deprecated until Rails 3.1 and not removed until Rails 3.2, however, the new API provides the following new methods that all return relations allowing them to be chained together:
1. where – provides conditions on the relation, what gets returned.
2. select – choose what attributes of the models you wish to have returned from the database
3. group – groups the relation on the attribute supplied.
4. having – provides an expression limiting group relations (GROUP BY constraint)
5. joins – joins the relation to another table
6. clause – provides an expression limiting join relations (JOIN constraint)
7. includes – includes other relations pre-loaded.
8. order – orders the relation based on the expression supplied
9. limit – limits the relation to the number of records specified.
10. lock – locks the records returned from the table.
11. readonly – returns an read only copy of the data
12. from – provides a way to select relationships from more than one table.
13. with_scope – and with_exclusive_scope now also return relations and so can be chained.
14. default_scope – also works with relations.