Reputation: 103
I have a rails 3 application that has 2 distinct layouts. One is for the end users, and the other is an admin console for managing the application.
That is usually very easy to handle since I can define a controller to use 1 layout. This works fine for most settings since they do not involve the end user. But some of my models will. For example an admin editing a user profile vs a user. To make this work should I have duplicate controllers for a model? One for the admin console and one for the users? Seems like I have thrown DRY out the window.
Thanks in advance for any feedback.
Upvotes: 2
Views: 87
Reputation: 22296
You do not need to duplicate your code. Just add an Authorization layer to your application and you can craft these features in your existent code. To achieve this I would recomend you the CanCan gem:
CanCan is an authorization library for Ruby on Rails which restricts what resources a given user is allowed to access. All permissions are defined in a single location (the Ability class) and not duplicated across controllers, views, and database queries.
You can see more and check the examples on the project page and there's also an Railscast.
Upvotes: 1
Reputation: 4686
It seems to me an admin might be doing different things with a user than an end user would be doing so different controllers may not be completely unDRY.
However, if they are completely the same and you just want to have a different layout, then you can just set your layout based on the current user. Something like this would work:
layout :determine_layout
def determine_layout
return 'admin' if current_user && current_user.admin?
'application'
end
Upvotes: 1