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

时间:2013-08-22 21:21:17

标签: ruby-on-rails ruby-on-rails-4 railstutorial.org

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

造成的

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

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


这是我在这里的第一个贡献,赞赏有关改进这个或未来帖子的反馈意见:)

Ruby on Rails教程:Learn Web Development with Rails 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

6 个答案:

答案 0 :(得分:21)

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

答案 1 :(得分:3)

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

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

您必须将images_controller.rb文件放在以下结构中:

-controllers
 |-brokers
   |-images_controller.rb

注意目录结构中images_controller.rb是代理的直接后代。

因此,为了让Rails找到你的类,不要在映射路由结构的properties内创建一个子目录brokers,它必须是经纪人的直接后代

答案 2 :(得分:2)

routes.rb我输入resource而不是resources

答案 3 :(得分:2)

如果有人遇到类似问题,请提供帮助:

我拼错了控制器,如果你在没有 s in products 的情况下输入它就错了:

错:

forEach()

右:

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

答案 4 :(得分:0)

在我的路线中:我有#34; /"而不是"#"对于所有" get",所以将其更改为"#"   得到所有' => '#店面all_items'

得到'分类' => '#店面items_by_category'

获得品牌推广' => '#店面items_by_brand'

修复了我的所有错误。

答案 5 :(得分: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