Routes in Rails3

Rails3 define routes in more efficient way

Routes for CRUD actions

1
     resources   :users

Also define for multiple resources

1
     resources :users,  :blogs, :books

Nested Routes

1
2
3
  resources :users do
    resources  :blogs
  end

Member and collection

1
2
3
4
  resources :blogs do
    :get => :preview, :on =>:member
    :get => :list, :on =>:collection
  end

if  we have more than one collection or member functions

1
2
3
4
5
6
7
8
9
10
resources  :blogs do
    member do
     :get  :preview
     :put :sort
    end
   collection do
     :get :list
     :get :detailed_info
   end
end

Named Routes

1
  match "history" => "site#index", :as => :history

Adding :as makes it a named route so that we can use history_path or history_url in our application.

Route for Root

1
  root   :to => "home#index"

Constraints and Parameters in routes

1
  match "search/:email(/:first_name/:last_name)" => "users#search",  :constraints => {:email => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i}

Here email is mandatory parameter and first_name and last is optional parameter and in constraints email format is defined