Ruby-on-rails 未初始化的常量“控制器名称”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13115662/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Uninitialized constant "Controller Name"
提问by Adam
I'm having an error with my routes/resources and controllers.
我的路由/资源和控制器出错。
I have the following in the routes.rb:
我在 routes.rb 中有以下内容:
# routes.rb
resources :users do
resource :schedule
end
And I have a schedule_controller.rb inside controllers/users/ set up as I think it should be:
我在controllers/users/里面有一个schedule_controller.rb,我认为应该是这样的:
class Users::ScheduleController < ApplicationController
# Controller methods here...
end
Running a rake:routes shows
运行 rake:routes 显示
user_schedule POST /users/:user_id/schedule(.:format) schedules#create
new_user_schedule GET /users/:user_id/schedule/new(.:format) schedules#new
edit_user_schedule GET /users/:user_id/schedule/edit(.:format) schedules#edit
GET /users/:user_id/schedule(.:format) schedules#show
PUT /users/:user_id/schedule(.:format) schedules#update
However, navigating to /users/:user_id/schedule is returning the following error:
但是,导航到 /users/:user_id/schedule 会返回以下错误:
uninitialized constant SchedulesController
My only thoughts on what the problem could be are that is has something to do with nested resources or declaring a single resource and I'm going wrong somewhere.
我对问题的唯一想法是与嵌套资源或声明单个资源有关,而我在某处出错了。
I'm using the helper
我正在使用助手
new_user_schedule_path(current_user)
when linking to my 'new' view.
链接到我的“新”视图时。
采纳答案by Andrew Marshall
It should be SchedulesController, not Users::ScheduleController. Controllers should only be namespaced when the route is namespaced with namespace. Controller names should also always be plural.
应该是SchedulesController,不是Users::ScheduleController。控制器应该只在路由命名空间为namespace. 控制器名称也应始终为复数。
What you're creating is a nestedresource, not a namespaced one.
回答by Ryan Bigg
Is the namespacing of the SchedulesControllerintentional? i.e. do you reallymean to do this?
命名空间是SchedulesController有意的吗?即你真的要这样做吗?
class Users::SchedulesController < ApplicationController
Or are you only doing that because schedules are a "sub-thing" from users?
或者你这样做只是因为时间表是用户的“子事物”?
The reason I ask this is because typically within Rails, nested resource controllers aren'tnamespaced. You would only namespace a controller if you wanted to modify the controllers in a special way under a namespace. A common example of this would be having some controllers under an admin namespace, inheriting from a BaseControllerwithin that namespace that would restrict only admins from acessing those controllers.
我问这个的原因是因为通常在 Rails 中,嵌套的资源控制器没有命名空间。如果您想在命名空间下以特殊方式修改控制器,则只能命名控制器。一个常见的例子是在 admin 命名空间下有一些控制器,继承自BaseController该命名空间内的a ,这将限制只有管理员访问这些控制器。
Option 1
选项1
If you didn't intentionallynamespace this controller, then you want to remove the Users::prefix from your controller, and move it back to app/controllers/schedules_controller.rb, the helpers back to app/helpers/schedules_helper.rband the views back to app/views/schedules. Perhaps you ran a generator which also generated a Users::Schedulemodel, which should also need to be renamed to Scheduleand moved back to app/models/schedule.rb.
如果您没有故意命名此控制器,那么您希望Users::从控制器中删除前缀,并将其移回app/controllers/schedules_controller.rb,将助手移回app/helpers/schedules_helper.rb,将视图移回app/views/schedules。也许您运行了一个生成器,该生成器也生成了一个Users::Schedule模型,该模型也应该重命名Schedule并移回app/models/schedule.rb.
Option 2
选项 2
If you did intentionallynamespace this controller, then you want to do this in your routes:
如果你故意命名这个控制器,那么你想在你的路由中这样做:
namespace :users do
resources :schedules
end
Leave everything that's been generated as it should be.
保留已生成的所有内容。
回答by Pshemski
In your routes.rb you need to specify the controller like this:
在您的 routes.rb 中,您需要像这样指定控制器:
resources :users do
resource :schedules, controller: 'users/schedules'
end
resources :users do
resource :schedules, controller: 'users/schedules'
end
回答by Hemant Kumar
replace resources :users to namespace :users
将资源 :users 替换为命名空间 :users
Because your schedule controller is inside users folder. class Users::ScheduleController < ApplicationController # Controller methods here... end
因为您的计划控制器位于用户文件夹中。class Users::ScheduleController < ApplicationController # 这里的控制器方法... end

