Reputation: 209
Ok a little more of a structural question here.
I have a rails application with three different types of users; those who view content, those who produce content, and administrators. there will be content and statistics and such that is available in different forms for each type of user e.g. a viewer cannot edit a news post but a producer or administrator can, etc etc.
I am trying to decide how to structure the controllers. I received advice to have a news action in the viewer controller, a news action in the producer controller, and a news action in the admin controller, all of which perform the respective functions of the type of user logged in. I am trying this approach right now but it seems incredibly messy. Would this be the best way to go, or should i instead be structuring this as a controller for news objects, a controller for this type of data, and so on, and instead direct users to the appropriate behavior within each controller based on their session?
I am getting quite bewildered with this as my current approach (the controller per user type approach) seems to be getting out of hand. Any advice is greatly appreciated.
Upvotes: 0
Views: 496
Reputation: 2859
I am currently working on a project where I use different controllers for different type of users (similar to your approach) I have 'admins/news_controller', 'producers/news_controller' and 'viewers/news_controller'. Public controller (the one in controllers/viewers
) has only show
and index
actions, and admins/news_controller
inherits from 'public' adding new, create, edit, update etc.
and authentication. However, this architecture was dictated by requirements, and may not be the best solution for you.
Another approach would be to have one news controller, one devise User model with three roles 'admin', 'producer' and 'viewer', and manage permissions for news
resource with CanCan gem from Ryan Bates. You can find it here: https://github.com/ryanb/cancan. Ryan also has a Railcast on CanCan: http://railscasts.com/episodes/192-authorization-with-cancan.
Hope it helps.
Upvotes: 1
Reputation: 160191
Totally depends. If it's just the type of news at depends on the user type, have scopes (or a lambda scope) in the news model to retrieve the correct news. It could also be part of the user model, so the has_many relationship knows what types of items to retrieve.
If the front-end functionality is different, then you'll want to render a different partial, or separate them completely, or use something like cells to have type-specific components, or...
Upvotes: 0