如何修复 rails 上 ruby 中的 'ActionController::RoutingError (uninitialized constant someController)' 错误 5

How to fix 'ActionController::RoutingError (uninitialized constant someController)' error in ruby on rails 5

我正在开发一个 API,它获取特定数据,创建一个 pdf 文件并使用 rails 将其发送到电子邮件中 ​​5. 设置好我的 routes/controllers/model 之后,我不断收到ActionController::RoutingError (uninitialized constant InfluencerreportsController).

我在网上冲浪试图找到解决方案,虽然有许多其他人也有同样的错误,但他们的 none 解决方案对我来说是可行的。据我所知,错误意味着我的文件名和控制器不匹配,但没有任何拼写错误。我知道显示 InfluencerreportsController 的错误消息,但我尝试将我的控制器更改为该名称,但没有任何作用。我是否也需要在其他位置更改它?

另一件值得一提的事情是,我为所有内容(控制器、模型和迁移)使用了 rails 生成,所以我没有自己创建任何文件。

这是我第一次处理 nested_attributes,所以我不确定我是否做对了,或者这是否会导致问题。

提前致谢!

这是我的项目目录:

由于缺乏声誉,我无法 post 图片,但我的控制器文件名为 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'

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

您的控制器名为 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 知道控制器的正确外壳。