###Comparison of route breakdown between Sinatra and Rails: ###
in Sinatra routes were written like this:
get '/' do
#some Ruby code
erb :index
end
This is taken apart in Rails. The first line now resides in config/routes.rb
and is decoupled from the Ruby code (now in controllers) and the view.
the basic syntax is:
get '/' => "pages#index"
get
is the HTTP verb'/'
is the URL'pages'
is the controller which Rails will look in'index'
is the instance method, or action, which Rails will then execute.
Rails will generate 7 restful routes for you with this command:
resources :(any name but likely a model). Here we'll call cats.
resources :cats
To see the generated routes (as well as all of your routes) run rake routes
in your terminal and you'll see a list of all of the routes available to your application.
The routes are that resources generated are:
prefix verb URI pattern controller#action
cats GET /cats(.:format) cats#index
POST /cats(.:format) cats#create
new_cat GET /cats/new(.:format) cats#new
edit_cat GET /cats/:id/edit(.:format) cats#edit
cat GET /cats/:id(.:format) cats#show
PATCH /cats/:id(.:format) cats#update
PUT /cats/:id(.:format) cats#update
DELETE /cats/:id(.:format) cats#destroy
- 'prefix' is a named path that you can use to call that route in the application. For instance, our
get '/'
example's prefix is 'root', and so to link to it elsewhere we only have to callroot_path
. To callget '/cats/:id/edit(.:format)'
we only have to typeedit_cat_path
. - The verb is the HTTP action verb.
- The URI pattern is what will appear in the address bar of the browser.
- the controller#action tells Rails which controller and what action method to look for.
If you don't require all of the standard routes you can generate using the only
and except
commands:
resources :cats, except: [:show, :edit, :update, :destroy]
resources :dogs, only: [:show, :edit, :update, :destroy]
In order to specify your application's root URL the syntax is:
root to: "controller#action"
Nested resources are used when one instance of a resource belongs to another resource. The syntax will look like this:
resources :posts do
resources :comments
end
You don't want to nest more than one resource deep or it can be too confusing to route. You can combine nested resources with the only
and except
commands, which is called shallow nesting:
resources :posts do
resources :comments, only: [:index, :new, :create]
end
resources :comments, only: [:show, :edit, :update, :destroy]
Member routes tell Rails that the actions defined in the block are specific to invidiual instances and not the collection as a whole, while collection routes tell Rails the opposite. These are useful for when you need named paths that aren't provided in the generated resources.
Member do syntax:
resources :post do
member do
get 'unicorns'
end
end
Generated route:
unicorns_post GET /post/:id/unicorns(.:format) post#unicorns
Collection do syntax:
resources :comments do
collection do
post 'search'
end
end
generated route:
search_comments POST /comments/search(.:format) comments#search
-
[A history of our commits while examining routes and practicing different actions](https://github.com/georgepradhan/rails_routes_demo/commits/master)
-
[Some more examples of collection versus member routes](accentuate.me/blog/?p=12)
-
[RailsGuide on Routing](http://guides.rubyonrails.org/routing.html)
-
[A nice run-down on routes in Rails 3](https://blog.engineyard.com/2010/the-lowdown-on-routes-in-rails-3)
-
[A line-by-line look at the differences between Rails 3.2.9.rc2 and Rails 4.0.0](https://github.com/natebird/Rails-3-2-9-to-Rails-4/commit/0a537c3b0ed4e36389c4af421e4d540b8e9f51a9)
-
[API Dock's Rails 3.2 guide on Routes](http://apidock.com/rails/ActionDispatch/Routing)