【问题标题】:ActionController::RoutingError: uninitialized constant MicropostsControllerActionController::RoutingError: 未初始化的常量 MicropostsController
【发布时间】:2013-08-25 19:25:53
【问题描述】:

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

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

【问题讨论】:

  • 更新:这是由于文件名拼写错误 ~/sample_app/app/controllers/microposts_controller.rb(原为 microposts_contoller.rb)
  • 不要评论,回答你自己的问题。
  • 同意你应该回答你的问题,这样用户就不必通读你的整个帖子和 cmets 就意识到这个问题已经解决了。
  • 对不起,我没有足够的积分 - 它不会让我再过 7 个小时回答。我会在顶部添加一些内容。

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


【解决方案1】:

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

我把控制器拼错了,如果你在产品中输入没有s那就错了:

错误:

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

对:

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

【讨论】:

    【解决方案2】:

    我在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
    

    【讨论】:

      【解决方案3】:

      在我的路线中:我使用“/”而不是“#”来表示所有“get”,因此将其更改为“#” 获取“所有”=>“店面#all_items”

      获取“分类”=>“店面#items_by_category”

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

      这解决了我所有的错误。

      【讨论】:

        【解决方案4】:

        routes.rb 中,我输入了resource 而不是resources

        【讨论】:

          【解决方案5】:

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

          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 找到你的类,不要在 brokers 内创建子目录 properties 映射路由结构,它必须是代理的直接后代

          【讨论】:

            【解决方案6】:

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

            【讨论】:

            • 感谢您将解决方案放在首位。我遇到了完全相同的问题,并且能够快速修复它。 :)
            • 我已经将我的controllers 文件夹移到了一个目录中?。
            猜你喜欢
            • 1970-01-01
            • 2018-09-04
            • 2016-01-16
            • 2016-01-19
            • 2017-08-05
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多