Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Route a controller to namespace :admin to /admin

I feel like this may be a dumb question, but it's late and my head is melting a bit.. So I appreciate the assistance.

I'm trying to map the url http://localhost:3000/admin to a dashboard controller but i'm epically failing. Maybe this isn't even possible or the completely wrong idea but anyway my routes looks like this and yes

namespace :admin do
  resources :dashboard, { :only => [:index], :path => '' }
  ...
end

and my simple dashboard_controller.rb

class Admin::DashboardController < ApplicationController
  before_filter :authenticate_user!
  filter_access_to :all

  def index
    @schools = School.all
  end
end

and my view is located in views/admin/dashboard/index.html.erb

thanks for any input

like image 634
shrunken_head Avatar asked Jan 27 '12 07:01

shrunken_head


2 Answers

If all you're trying to do is route /admin to that dashboard controller, then you're overcomplicating it by namespacing it like that.

Namespacing with a nested resource like that would mean that it would be /admin/dashboards for the :index action instead of having a clean /admin route (and you can verify that by running rake routes at the command line to get a list of your routes).

Option 1: You meant to namespace it like that

# putting this matched route above the namespace will cause Rails to 
# match it first since routes higher up in the routes.rb file are matched first
match :admin, :to => 'admin/dashboards#index'
namespace :admin do
  # put the rest of your namespaced resources here
  ...
end

Option 2: You didn't mean to namespace it like that

Route:

match :admin, :to => 'dashboards#index'

Controller:

# Remove the namespace from the controller
class DashboardController < ApplicationController
  ...
end

Views should be moved back to:

views/dashboards/index.html.erb

More info: http://guides.rubyonrails.org/routing.html

like image 191
iwasrobbed Avatar answered Oct 22 '22 06:10

iwasrobbed


Regarding to http://guides.rubyonrails.org/routing.html I prefer this

namespace :admin do
  root to: "admin/dashboards#index"
  resources :dashboard
end
like image 34
DonPaulie Avatar answered Oct 22 '22 05:10

DonPaulie