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

27
更新:这是由于文件名拼写错误引起的。
正确的:
〜/sample_app/app/controllers/microposts_controller.rb 不正确的:
〜/sample_app/app/controllers/microposts_contoller.rb
这是我的第一次贡献,欢迎提供反馈以改进此次或未来的发帖:)
Ruby on Rails教程:学习使用Rails 4进行Web开发 在阅读第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

更新:这是由于文件名拼写错误~/sample_app/app/controllers/microposts_controller.rb(应为microposts_controller.rb)所致。 - 8legged
3
不要评论,回答你自己的问题。 - fotanus
1
同意你应该回答自己的问题,这样用户就不必在阅读完整个帖子和评论之前才意识到这个问题已经解决了。 - khollenbeck
抱歉,我没有足够的积分 - 它不会让我在另外7个小时内回答。我会在顶部添加一些内容。 - 8legged
在我的情况下,“拼写错误”是我没有使用模型名称的复数形式。 - bugloaf
6个回答

29

这是由于一个拼写错误的文件名 ~/sample_app/app/controllers/microposts_controller.rb (应为microposts_contoller.rb) 导致的。


我很感激你把解决方案放在最顶部。我遇到了完全相同的问题,但是通过你的解决方案我很快就解决了。 :) - Ege Ersoz
1
我已将我的 controllers 文件夹移到了上一级目录。 - IIllIIll

6

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

启动POST“/brokers/properties/5/images/upload”for ...

ActionController :: RoutingError(未初始化的常量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

您必须将您的images_controller.rb文件放置在以下结构中:
-controllers
 |-brokers
   |-images_controller.rb

请注意目录结构中 images_controller.rb 是经纪人的直接子代。

因此,为了让Rails找到您的类,请不要在 brokers 中创建名为 properties 的子目录来映射路由结构,它必须是 brokers 的直接子代。


3

如果有人遇到类似问题,我来帮忙解决一下:

我的控制器拼写错误了,如果你在输入“products”时没有s,就会出错:

错误的写法:

get '/my_products', to: 'product#my_products'

正确的:

get '/my_products', to: 'products#my_products'

2
routes.rb文件中,我输错了单词resources,应该是resource

0
在我的路由中:我所有的“get”都使用了“/”而不是“#”,所以将其更改为“#” get 'all' => 'storefront#all_items'

get 'categorical' => 'storefront#items_by_category'

get 'branding' => 'storefront#items_by_brand'

这样就解决了我所有的错误。


0

我错误地将以下内容包含在我的application_controller.rb中:

正确:include ActionController::MimeResponds

错误:include ActionController::MimeResponse

# /controllers/api/v1/application_controller.rb

module Api
  module V1
    class ApplicationController < ActionController::API
      include ActionController::MimeResponds
    end
  end
end

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接