Redirect/call method from another controller in a different namespace in Rails

Viewed 3521

I have a pretty typical situation where I have a '/dashboard' which should render a different view for different user roles (i.e. client, admin, etc.).

I am open to more elegant suggestions but my thought was to have one route definition for dashboard like so:

routes.rb

resource :dashboard

and to have a dashboards_controller.rb like so:

class DashboardsController < ApplicationController
  def show
    if current_user.has_role?('sysadmin')
      // show system admin dashboard
    elsif
      // show a different dashboard

    // etc
    end
  end
end

Now I would like that each dashboard gets built in its role specific namespaced dashboard_controller, ex: controllers/admin/dashboard_controller.rb. This way, each dashboard can be appropriately built up in the right place.

The way I am trying to do this is to redirect from my main dashboards_controller to the admin/dashboard_controller like so:

redirect_to :controller => 'admin/dashboard_controller', :action => 'index'

But it is not working, presumably because I am not sure how to reference a namespaced controller from here.

How can I achieve this?

(If there is a more elegant solution I am open but I thought this was pretty good).

I am using devise and cancancan.

2 Answers
Related