2012-01-27 81 views
7

我觉得这可能是一个愚蠢的问题,但已经很晚了,我的脑袋正在融化一点。所以我很感激这个帮助。将控制器路由到名称空间:admin到/ admin

我想映射网址http://localhost:3000/admin到仪表板控制器,但我在史诗上失败。也许这甚至不可能或完全错误的想法,但无论如何我的路线是这样的,是的

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

和我的简单dashboard_controller.rb

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

    def index 
    @schools = School.all 
    end 
end 

和我的看法是位于景色/管理/dashboard/index.html.erb

感谢任何输入

回答

9

如果你正在试图做的一切都是为了那个仪表盘控制器航线/admin,那么你在通过像这样命名它来使它复杂化。

有了这样的嵌套资源命名空间将意味着,这将是/admin/dashboards:index行动而不是有干净/admin路线(你可以验证通过在命令行中运行rake routes让你的路由列表) 。

选择1:你的意思的命名空间一样,

# 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 

选项2:你不是故意的命名空间一样,

路线:

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

控制器:

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

意见应该搬回:

views/dashboards/index.html.erb 

更多信息:http://guides.rubyonrails.org/routing.html

+0

比ks被抢!选项1固定了我。 – 2012-01-27 15:35:36

+1

这些仅适用于将特定路径/管理员映射到特定操作仪表板#索引。他们也不会将map/admin /:action映射到仪表盘#:action。 – cilphex 2015-03-08 01:44:09

0

试试这个:

namespace :admin do 
    root to: 'users#index' # whatever. Just don't start with /admin 
    #resources :dashboards <= REMOVE THIS LINE ! 
end 
相关问题