Sunday 23 July 2017

Polymorphic routing to get the path of different resource

Suppose that we have three types of resources and we have three model as Employee, Employer and Manager.

Now we also have separate dashboard action in controller and After sign in we have to redirect to dashboard path according to the resource.

employees controller -

 
 class EmployeesController < ApplicationController
  
   def dashboard
         # some_code
   end 
 end


employers controller -


  
 class EmployersController < ApplicationController
  
   def dashboard
         # some_code
   end 
 end


managers controller -


  
 class ManagersController < ApplicationController
  
   def dashboard
         # some_code
   end 
 end


And routes would look like this:


 resources :employees do
   member do
     get 'dashboard'
   end
 end

 resources :employers do
   member do
     get 'dashboard'
   end
 end

 resources :managers do
   member do
     get 'dashboard'
   end
 end


In this scenario we can use switch case:


 case current_user
 when Manager
   dashboard_manager_path
 when Employer
   dashboard_employer_path
 when Employee
   dashboard_employee_path
 end


Don't you think it can be re-factor!

Rails provides polymorphic_path that makes polymorphic url generation much easier and simpler. There is also a method named polymorphic_url which is the same as the polymorphic_path except that polymorphic_url generate the full url including the host name.


These methods are useful when you want to generate the correct URL or path to a RESTful resource without having to know the exact type of the record in question.

It also provides support for nested resources.

So, we can re-factor our code using ploymorphic_path helper method:


 polymorphic_path([:dashboard, current_user])

5 comments:

  1. Great blogger. its is very very helpful for all of us and I never get bored while reading your blogger because, they are
    becomes a more and more interesting from the starting lines until the end. get more on Ruby on Rails Online Training

    ReplyDelete
  2. After reading this blog i very strong in this topics and this blog really helpful to all.. Ruby on Rails Online Course

    ReplyDelete
  3. AldaimSolutions as an offshore e-Commerce website development company provides a wide range of online business solutions in web and mobile space.Popular rails gemsRails is basically a web application framework, which is consist of everything needs to create database baked web application. It helps the developers to create websites and applications by providing structures for all codes written by them. Moreover, common repetitive tasks are simplified with the help of this technology.
    Websites made with PHP and Shopify
    Best Ecommerces Company Audacity24
    Websites made with PHP and Shopify
    Best ruby APIS
    React native and React Js
    Node Js and React Js

    ReplyDelete
  4. I like your post very much. It is very much useful for my research. I hope you to share more info about this. Keep posting ruby on rails training

    ReplyDelete
  5. Very informative. I hope you to share more info about this. Keep posting. You can also refer more Ruby Language,
    Ruby Language or articles on other language like
    Python, feel free to look for inspiration.

    ReplyDelete