Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.7k views
in Technique[技术] by (71.8m points)

ruby on rails - Uninitialized constant "Controller Name"

I'm having an error with my routes/resources and controllers.

I have the following in the 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:

class Users::ScheduleController < ApplicationController

  # Controller methods here...

end

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

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.

I'm using the helper

new_user_schedule_path(current_user)

when linking to my 'new' view.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...