I'm working on an application with a namespacing (admin section). Is there a DRY solution for not creating 2 controllers? Because I will need to create an public user controller and a admin user controller to manage the users.
-
I'm a bit confused by your question. Is it the case that there are two controllers, one for viewing a user's profile and one for allowing an administrator to edit it, and you'd like to merge the two? Or is it the case that you have the notion of "user" and the notion of "admin" and you'd like to combine them using some sort of role-based model?Brandon Yarbrough– Brandon Yarbrough2011-08-08 20:10:23 +00:00Commented Aug 8, 2011 at 20:10
-
The first. I want a solution for merging them.Databaas– Databaas2011-08-08 20:12:35 +00:00Commented Aug 8, 2011 at 20:12
Add a comment
|
2 Answers
How about inheriting the user controller? I use it myself (for images) and it suits me nicely:
# file: apps/controllers/images_controller.rb
class ImagesController < ApplicationController
# image code (to show the image for example)
end
# file: apps/controllers/admin/images_controller.rb
class Admin::ImagesCOntroller < ImagesController
# additional admin code (to delete the image for example)
end
You might consider rendering the page with optional "edit" buttons. For example, something like this:
Name: <%= @user.name %>
<% if @user.admin? %>
<% form_for @user do |f| %>
Editing stuff
<% end %>
<% end %>
That way, a user only sees it as a page, but an admin sees additional controls that allows them to edit the field. P.S. Make sure in your controller that you are checking to make sure it's an admin that is calling the update call.