My Rails app /index path should show one page to logged in users, an entirely different page to admin users, and a totally different one for users who aren’t logged in.
So, one controller (HomeController), one action (index), three views.
I can implement this in two ways:
- Insert
ifclauses intoviews/home/index.html.erbmaking it do the right thing. - Put the conditional logic into the controller somehow (make the controller find out who the user is) and then render a different layout accordingly, as described under 2.2.12 Finding Layouts in http://guides.rubyonrails.org/layouts_and_rendering.html.
I have a strong feeling the latter solution is the right one. But I’m not sure how to implement it, what with the user controller being a totally different controller. How can my home controller find out who the current user is to decide what view to render?
You have access to current_user object in all controllers, to check whether user is logged-in or not. Also we can get user role from the current_user object by
You can pass this role name to render partial as file name(Keep different files).