Ruby-on-rails ActionController::RoutingError: 未初始化的常量 MicropostsController
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18391048/
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
ActionController::RoutingError: uninitialized constant MicropostsController
提问by 8legged
Update: This was due to a misspelled file name
更新:这是由于拼写错误的文件名
correct:~/sample_app/app/controllers/microposts_controller.rb
正确的:~/sample_app/app/controllers/microposts_controller.rb
incorrect:~/sample_app/app/controllers/microposts_contoller.rb
不正确:~/sample_app/app/controllers/microposts_contoller.rb
This is my first contribution here, feedback on improving this or future postings is appreciated :)
这是我在这里的第一个贡献,感谢有关改进此帖子或未来帖子的反馈:)
Ruby on Rails Tutorial: Learn Web Development with Rails 4
Ruby on Rails 教程:使用 Rails 4 学习 Web 开发
While working through chapter 10.3, I got stuck. In the end, a misspelled file name had me chasing ghosts for a few days.
在完成第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
回答by 8legged
This was due to a misspelled file name ~/sample_app/app/controllers/microposts_controller.rb (was microposts_contoller.rb)
这是由于拼写错误的文件名 ~/sample_app/app/controllers/microposts_controller.rb (是 microposts_contoller.rb)
回答by juliangonzalez
This can also happen if you have a nested route mapping a nested directory:
如果您有映射嵌套目录的嵌套路由,也会发生这种情况:
Started POST "/brokers/properties/5/images/upload" for ...
Started POST "/brokers/properties/5/images/upload" for ...
ActionController::RoutingError (uninitialized constant Brokers::ImagesController):
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
You Must place your images_controller.rbfile with the following structure:
您必须放置images_controller.rb具有以下结构的文件:
-controllers
|-brokers
|-images_controller.rb
Notice in the directory structure images_controller.rbis direct descendant of brokers.
目录结构中的注意事项images_controller.rb是经纪人的直接后代。
So in order to let Rails find your class don't create a subdirectory propertiesinside brokersmapping the route structure, it has to be direct descendent of brokers
因此,为了让 Rails 找到您的类,不要properties在brokers映射路由结构的内部创建子目录,它必须是 brokers 的直接后代
回答by thedanotto
In routes.rbI typed resourceinstead of resources
在routes.rb我输入resource而不是resources
回答by Ahmed Elkoussy
Just to help if someone gets stuck with a similar issue:
只是为了在有人遇到类似问题时提供帮助:
I misspelled the controller, if you type without the s in productsit was wrong:
我拼错了控制器,如果你在产品中没有输入s,那就是错误的:
Wrong:
错误的:
get '/my_products', to: 'product#my_products'
Right:
对:
get '/my_products', to: 'products#my_products'
回答by Wei Liu
in my routes: i had "/"instead of "#" for all the "get", so change that to "#" get 'all' => 'storefront#all_items'
在我的路线中:我有“/”而不是“#”来表示所有“get”,因此将其更改为“#”get 'all' => 'storefront#all_items'
get 'categorical' => 'storefront#items_by_category'
获得“分类”=>“店面#items_by_category”
get 'branding' => 'storefront#items_by_brand'
获得“品牌”=>“店面#items_by_brand”
that fixed all my errors.
修复了我所有的错误。
回答by Elijah Murray
I had incorrectly included the below in my application_controller.rb
我错误地将以下内容包含在我的 application_controller.rb
Correct:include ActionController::MimeResponds
正确的:include ActionController::MimeResponds
Incorrect:include ActionController::MimeResponse
不正确:include ActionController::MimeResponse
# /controllers/api/v1/application_controller.rb
module Api
module V1
class ApplicationController < ActionController::API
include ActionController::MimeResponds
end
end
end

