Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Customizing Routes with resources

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 324
    Comment on it

    Hi Friends,

    In my previous blog Routes in Rails using Objects, I explained about using routes with objects. Today I am going to tell you about how we can customize the routes that are created using resources. As we know whenever we mention resources in routes it creates 7 routes for plural names and 6 for singular names  like this:

    resources :articles
    
    
    $ rake routes
          Prefix Verb   URI Pattern                  Controller#Action
        articles GET    /articles(.:format)          articles#index
                 POST   /articles(.:format)          articles#create
     new_article GET    /articles/new(.:format)      articles#new
    edit_article GET    /articles/:id/edit(.:format) articles#edit
         article GET    /articles/:id(.:format)      articles#show
                 PATCH  /articles/:id(.:format)      articles#update
                 PUT    /articles/:id(.:format)      articles#update
                 DELETE /articles/:id(.:format)      articles#destroy
    

     

     resource :articles
    
    $ rake routes
           Prefix Verb   URI Pattern              Controller#Action
         articles POST   /articles(.:format)      articles#create
     new_articles GET    /articles/new(.:format)  articles#new
    edit_articles GET    /articles/edit(.:format) articles#edit
                  GET    /articles(.:format)      articles#show
                  PATCH  /articles(.:format)      articles#update
                  PUT    /articles(.:format)      articles#update
                  DELETE /articles(.:format)      articles#destroy
    

    There are multiple ways we can customize these resource type routes. Some of them are:

    a)  Using constraints:

    Anyone can hit anything in a route but if we want the route to be constraint or in other words if we want to allow only specific type of string as an id. We can give a regex as a constraint in the routes like this. Suppose we want to allow only numeric digits in id we can specify the route as:

    resources :articles, constraints: { id: /[0-9]+/ }
    
    
    $ rake routes
          Prefix Verb   URI Pattern                  Controller#Action
        articles GET    /articles(.:format)          articles#index
                 POST   /articles(.:format)          articles#create
     new_article GET    /articles/new(.:format)      articles#new
    edit_article GET    /articles/:id/edit(.:format) articles#edit {:id=>/[0-9]+/}
         article GET    /articles/:id(.:format)      articles#show {:id=>/[0-9]+/}
                 PATCH  /articles/:id(.:format)      articles#update {:id=>/[0-9]+/}
                 PUT    /articles/:id(.:format)      articles#update {:id=>/[0-9]+/}
                 DELETE /articles/:id(.:format)      articles#destroy {:id=>/[0-9]+/}
    


    b) Overriding named helpers:

    We can also modify the named helpers by overriding the as: option with the routes like this:

    resources :articles
    ## By default all route helpers will contain article_path or articles_path.
    
    resources :articles
    resources :photos, as: 'blogs'
    
    
      $ rake routes
          Prefix Verb   URI Pattern                  Controller#Action
        articles GET    /articles(.:format)          articles#index
                 POST   /articles(.:format)          articles#create
     new_article GET    /articles/new(.:format)      articles#new
    edit_article GET    /articles/:id/edit(.:format) articles#edit
         article GET    /articles/:id(.:format)      articles#show
                 PATCH  /articles/:id(.:format)      articles#update
                 PUT    /articles/:id(.:format)      articles#update
                 DELETE /articles/:id(.:format)      articles#destroy
           blogs GET    /photos(.:format)            photos#index
                 POST   /photos(.:format)            photos#create
        new_blog GET    /photos/new(.:format)        photos#new
       edit_blog GET    /photos/:id/edit(.:format)   photos#edit
            blog GET    /photos/:id(.:format)        photos#show
                 PATCH  /photos/:id(.:format)        photos#update
                 PUT    /photos/:id(.:format)        photos#update
                 DELETE /photos/:id(.:format)        photos#destroy

     

    c) Change the path names for edit, new etc

    We can update the method names in paths by overriding the :path_names option like this:

    resources :articles, path_names: { new: 'brand_new' }
    
    # so new will be replaced like
    /articles/new  ==>  /articles/brand_new

    d) Changing Named Route Parameters:

    As we all now for edit update like actions the url for the resource is like:

    /articles/:id/edit(.:format)

    where the identifier is by default id but if we want to change it we can change it by changing the resource like:

    resources :articles, param: :my_param
    
    #=> route will be like
    
    /articles/:my_param/edit(.:format)

    e) Using only and except in routes:

    As I told earlier that as soon as we add a resource in route it creates 6 (for singular) and 7 (for plural) routes for that resource, but we can use only and except options to restrict them

    resources :articles, only: [:index, :create]
    ## will create routes for only index and create actions
    
    ## Similarly
    resources :articles, except: [:update, :destroy, :edit]
    ## will create routes except update, destroy and edit actions
    

    f) changing the controller name:

    By default when routes are added, they use the same controller name as the resource but if we want the route to point to a different controller's action we can do so like this

    resources :articles, controller: 'blogs'
    
    $ rake routes
          Prefix Verb   URI Pattern                  Controller#Action
        articles GET    /articles(.:format)          blogs#index
                 POST   /articles(.:format)          blogs#create
     new_article GET    /articles/new(.:format)      blogs#new
    edit_article GET    /articles/:id/edit(.:format) blogs#edit
         article GET    /articles/:id(.:format)      blogs#show
                 PATCH  /articles/:id(.:format)      blogs#update
                 PUT    /articles/:id(.:format)      blogs#update
                 DELETE /articles/:id(.:format)      blogs#destroy
    

    Hope you liked this. For information related to rails routes go this link

    http://guides.rubyonrails.org/routing.html

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: