1

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.

2
  • 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? Commented Aug 8, 2011 at 20:10
  • The first. I want a solution for merging them. Commented Aug 8, 2011 at 20:12

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

2 Comments

I'm inheriting the Admin::BaseController at the moment. But I think what you're suggesting is the best way.
Oops spotted an error: of course ImagesController has its own (usual) inheritance
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.