ActionController :: RoutingError:未初始化的常量MicropostsController

8le*_*ged 16 ruby-on-rails railstutorial.org ruby-on-rails-4

更新:这是由于拼写错误的文件名

正确:
~/sample_app/app/controllers/microposts_controller.rb

不正确的:
~/sample_app/app/controllers/microposts_contoller.rb


这是我在这里的第一个贡献,赞赏改善这个或未来的贴子感谢:)

Ruby on Rails教程:使用Rails学习Web开发4

在完成第10.3章的过程中,我陷入困境.最后,一个拼写错误的文件名让我追了几天幽灵.

$ rspec spec/requests/authentication_pages_spec.rb
No DRb server is running. Running in local process instead ...
...FF................

Failures: 

1) Authentication authorization for non-signed-in users in the Microposts controller submitting to the create action 
Failure/Error: before { post microposts_path } 
ActionController::RoutingError: 
uninitialized constant MicropostsController 
# ./spec/requests/authentication_pages_spec.rb:93:in `block (6 levels) in ' 

2) Authentication authorization for non-signed-in users in the Microposts controller submitting to the destroy action 
Failure/Error: before { delete micropost_path(FactoryGirl.create(:micropost)) } 
ActionController::RoutingError: 
uninitialized constant MicropostsController 
# ./spec/requests/authentication_pages_spec.rb:98:in `block (6 levels) in ' 

Finished in 0.92253 seconds 
21 examples, 2 failures 

Failed examples: 

rspec ./spec/requests/authentication_pages_spec.rb:94 # Authentication authorization for non-signed-in users in the Microposts controller submitting to the create action 
rspec ./spec/requests/authentication_pages_spec.rb:99 # Authentication authorization for non-signed-in users in the Microposts controller submitting to the destroy action
Run Code Online (Sandbox Code Playgroud)

8le*_*ged 21

这是由于拼写错误的文件名〜/ sample_app/app/controllers/microposts_controller.rb(是microposts_contoller.rb)


jul*_*lez 6

如果您有映射嵌套目录的嵌套路由,也可能会发生这种情况:

Started POST "/brokers/properties/5/images/upload" for ...

ActionController::RoutingError (uninitialized constant Brokers::ImagesController):

namespace :brokers do
  resources :properties, only: [] do
    collection do
      post 'upload'
    end
    member do
      resources :images, only: [] do
        collection do
          post 'upload'
        end
      end
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

images_controller.rb您必须按以下结构放置文件:

-controllers
 |-brokers
   |-images_controller.rb
Run Code Online (Sandbox Code Playgroud)

注意目录结构中images_controller.rb是经纪人的直系后代。

因此,为了让 Rails 找到您的类,不要propertiesbrokers映射路由结构中创建子目录,它必须是代理的直接后代