25

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

正确的:
~/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
4

6 回答 6

28

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

于 2013-08-28T08:44:27.467 回答
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

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

-controllers
 |-brokers
   |-images_controller.rb

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

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

于 2017-02-24T23:45:26.460 回答
3

如果有人遇到类似问题,只是为了帮助:

我拼错了控制器,如果您在 products 中键入没有 s是错误的:

错误的:

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

对:

get '/my_products', to: 'products#my_products'
于 2018-04-07T20:11:12.630 回答
2

routes.rb我输入resource而不是resources

于 2017-04-14T14:15:39.853 回答
0

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

得到“分类”=>“店面#items_by_category”

获得“品牌”=>“店面#items_by_brand”

这解决了我所有的错误。

于 2017-04-25T14:09:56.683 回答
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
于 2017-05-24T21:31:30.790 回答