将控制器路由到命名空间:admin to / admin

时间:2012-01-27 07:14:57

标签: ruby-on-rails ruby-on-rails-3.1 routing

我觉得这可能是一个愚蠢的问题,但现在已经很晚了,我的脑袋正在融化......所以我很感激你的帮助。

我正在尝试将网址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

我的视图位于views / admin / dashboard / index.html.erb

感谢任何输入

3 个答案:

答案 0 :(得分:10)

如果你要做的就是将/admin路由到该仪表板控制器,那么你就会通过命名它来使它过于复杂。

使用类似嵌套资源的命名空间意味着/admin/dashboards操作:index而不是干净的/admin路由(并且您可以通过运行{{ 1}}在命令行获取路由列表)。

选项1:您的意思是将其命名为

rake routes

选项2:您并不是要将其命名为

路线:

# 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

控制器:

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

视图应该移回:

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

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

答案 1 :(得分:3)

关于http://guides.rubyonrails.org/routing.html我更喜欢这个

namespace :admin do
  root to: "admin/dashboards#index"
  resources :dashboard
end

答案 2 :(得分:0)

试试这个:

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