问题描述
我的路由/资源和控制器出现错误.
I'm having an error with my routes/resources and controllers.
我在 routes.rb 中有以下内容:
I have the following in the routes.rb:
# routes.rb
resources :users do
resource :schedule
end
我在controllers/users/里面有一个schedule_controller.rb,我认为应该是这样的:
And I have a schedule_controller.rb inside controllers/users/ set up as I think it should be:
class Users::ScheduleController < ApplicationController
# Controller methods here...
end
运行 rake:routes 显示
Running a rake:routes shows
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
但是,导航到/users/:user_id/schedule 返回以下错误:
However, navigating to /users/:user_id/schedule is returning the following error:
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.
我正在使用助手
new_user_schedule_path(current_user)
链接到我的新"视图时.
when linking to my 'new' view.
推荐答案
应该是 SchedulesController
,而不是 Users::ScheduleController
.只有当路由是 命名空间时,控制器才应该被命名空间命名空间
.控制器名称也应始终为复数.
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.
您正在创建的是一个嵌套资源,不是命名空间的.
What you're creating is a nested resource, not a namespaced one.
这篇关于未初始化的常量“控制器名称"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!