Skip to content

Instantly share code, notes, and snippets.

@RainMonster
Last active December 23, 2015 07:49
Show Gist options
  • Save RainMonster/6603915 to your computer and use it in GitHub Desktop.
Save RainMonster/6603915 to your computer and use it in GitHub Desktop.
Short introduction to routes in Rails.

###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.

What does the resources method do in routes.rb? What routes does it add and why?

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 call root_path. To call get '/cats/:id/edit(.:format)' we only have to type edit_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.

How can I configure what routes the resources method generates?

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]

How do I specify my application's root URL?

In order to specify your application's root URL the syntax is:

    root to: "controller#action"

What are nested resources? Why do I care about them? Are they worth understanding at the outset?

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]

What are "member" routes? What are "collection" routes?

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

Here are some links for further reading:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment