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 -
employers controller -
managers controller -
And routes would look like this:
In this scenario we can use switch case:
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:
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])