【问题标题】:How to fix 'ActionController::RoutingError (uninitialized constant someController)' error in ruby on rails 5如何修复 ruby​​ on rails 5 中的“ActionController::RoutingError (uninitialized constant someController)”错误
【发布时间】:2019-05-28 04:06:06
【问题描述】:

我正在开发一个 API,它获取特定数据、创建 pdf 并使用 rails 5 在电子邮件中发送。设置路由/控制器/模型后,我不断收到ActionController::RoutingError (uninitialized constant InfluencerreportsController).

我在网上冲浪试图找到解决方案,虽然还有很多其他人有同样的错误,但他们的解决方案都没有为我解决。据我所知,该错误意味着我的文件名和控制器不匹配,但任何地方都没有拼写错误。我知道错误消息说InfluencerreportsController,但我尝试将我的控制器更改为该名称,但什么也没做。我可能还需要在其他地方更改它吗?

另一件值得一提的是,我对所有内容(控制器、模型和迁移)都使用了 rails generate,因此我没有自己创建任何文件。

这是我第一次与nested_attributes 打交道,所以我不确定我是否做得正确,或者这是否会导致问题。

提前谢谢你!

这是我的项目目录:

由于缺乏声誉,我无法发布图片,但我的控制器文件名为 influencer_reports_controller.rb

influencer_reports_controller.rb:

class InfluencerReportsController < ApplicationController

  def create
    @report = InfluencerReport.create!(influencerreport_params)
    json_response(@report, :created)
  end

  private

  def influencerreport_params
    params.require(:influencerreport).permit(:instagram_handle,
                                             :email,
                                             :city,
                                             :post_price_by_category,
                                       :post_price_by_category_engagements,
                                         :post_price_by_avg_engagements,
                                         photos_attributes: [
                                           :industry,
                                           :likes,
                                           :comments
                                           ])
  end
end

influencer_report_model.rb:

class InfluencerReport < ApplicationRecord
  # model assocation
  has_many :photos, inverse_of: :influencerreport

  # validations
  validates_presence_of :instagram_handle,
                        :email,
                        :city,
                        :post_price_by_category,
                    :post_price_by_category_engagements,
                    :post_price_by_avg_engagements

  accepts_nested_attributes_for :photos
end

routes.rb:

Rails.application.routes.draw do
  # For details on the DSL available within this file, see 
http://guides.rubyonrails.org/routing.html

  resources :influencerreports, only: [:create] do
    resources :photos, only: [:create]
  end
end

错误信息:

Started POST "/influencerreports" for 127.0.0.1 at 2018-12-29 06:03:07 -0600

ActionController::RoutingError (uninitialized constant InfluencerreportsController):

activesupport (5.0.7.1) lib/active_support/inflector/methods.rb:268:in `const_get'
activesupport (5.0.7.1) lib/active_support/inflector/methods.rb:268:in `block in constantize'
activesupport (5.0.7.1) lib/active_support/inflector/methods.rb:266:in `each'
activesupport (5.0.7.1) lib/active_support/inflector/methods.rb:266:in `inject'
activesupport (5.0.7.1) lib/active_support/inflector/methods.rb:266:in `constantize'
actionpack (5.0.7.1) lib/action_dispatch/http/request.rb:81:in `controller_class'
actionpack (5.0.7.1) lib/action_dispatch/routing/route_set.rb:44:in `controller'
actionpack (5.0.7.1) lib/action_dispatch/routing/route_set.rb:30:in `serve'
actionpack (5.0.7.1) lib/action_dispatch/journey/router.rb:39:in `block in serve'
actionpack (5.0.7.1) lib/action_dispatch/journey/router.rb:26:in `each'
actionpack (5.0.7.1) lib/action_dispatch/journey/router.rb:26:in `serve'
actionpack (5.0.7.1) lib/action_dispatch/routing/route_set.rb:727:in `call'
rack (2.0.6) lib/rack/etag.rb:25:in `call'
rack (2.0.6) lib/rack/conditional_get.rb:38:in `call'
rack (2.0.6) lib/rack/head.rb:12:in `call'
activerecord (5.0.7.1) lib/active_record/migration.rb:553:in `call'

错误消息还有更多内容,但我觉得没有必要发布。

【问题讨论】:

    标签: controller routes ruby-on-rails-5 nested-attributes rails-api


    【解决方案1】:

    您的控制器名为InfluencerReportsController,您的错误消息显示应用正在寻找InfluencerreportsController 控制器。尝试将路线更改为:

    Rails.application.routes.draw do
      # For details on the DSL available within this file, see 
    http://guides.rubyonrails.org/routing.html
    
      resources :influencer_reports, only: [:create] do
        resources :photos, only: [:create]
      end
    end
    

    这应该让 rails 知道控制器的正确外壳。

    【讨论】:

    • 非常感谢!我不敢相信我没有意识到如果没有下划线,它会认为影响力报告是一个词..
    猜你喜欢
    • 1970-01-01
    • 2020-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-11
    • 1970-01-01
    相关资源
    最近更新 更多