尝试在Rails路由中使用命名空间时,获取ActionController :: RoutingError未初始化的常量

时间:2014-03-27 12:04:50

标签: ruby-on-rails ruby ruby-on-rails-4

我有两个控制器dashboardposts。我试图将帖子的:new, :create, :destroy, :edit, :update个动作放在dashboard个网址中,就像这个dashboard/posts/new一样。但我正在与new控制器中的posts动作进行对话。不在dash控制器中。这是我的routes.rb文件:

resources :posts, :except => [:new, :create, :destroy, :edit, :update]

get 'dashboard', to: 'dash#show'

namespace :dashboard do
   resources :posts, :only => [:new, :create, :destroy, :edit, :update]
end
get 'dashboard/posts', to: 'dash#posts'

有问题的两个控制器是:dashposts

现在,当我尝试访问http://localhost:3000/dashboard/posts/new时,它说:

ActionController::RoutingError at /dashboard/posts/new
uninitialized constant Dashboard

如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

假设您有两个控制器DashControllerPostsController。 并且您想访问namespace范围内的一些帖子路线,然后您可以按如下方式定义路线:

resources :posts, :only => [:index, :show]

get 'dashboard', to: 'dash#show'

scope :dashboard do
   resources :posts, controller: :posts ,:only => [:new, :create, :destroy, :edit, :update]
end
get 'dashboard/posts', to: 'dash#posts'

答案 1 :(得分:0)

无法找到常量“仪表板”。我认为问题在于您有时会使用dash,有时会使用dashboard