Databaas
Databaas

Reputation: 51

Double controllers when using namespace in rails?

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.

Upvotes: 1

Views: 285

Answers (2)

Veger
Veger

Reputation: 37915

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

Upvotes: 1

Brandon Yarbrough
Brandon Yarbrough

Reputation: 38399

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.

Upvotes: 0

Related Questions