controller - 如何修复 ruby on rails 5 中的 'ActionController::RoutingError (uninitialized constant someController)' 错误
问题描述
我正在开发一个获取特定数据的 API,创建一个 pdf 并使用 rails 5 在电子邮件中发送它。在设置我的路由/控制器/模型后,我不断得到ActionController::RoutingError (uninitialized constant InfluencerreportsController).
我在网上冲浪试图找到解决方案,虽然还有很多其他人有同样的错误,但他们的解决方案都没有为我解决。据我所知,该错误意味着我的文件名和控制器不匹配,但任何地方都没有拼写错误。我知道错误消息说InfluencerreportsController
,但我尝试将我的控制器更改为该名称,但什么也没做。我可能还需要在其他地方更改它吗?
值得一提的另一件事是,我对所有内容(控制器、模型和迁移)都使用了 rails generate,所以我自己没有创建任何文件。
这是我第一次处理nested_attributes
,所以我不确定我是否做得正确,或者这是否会导致问题。
先感谢您!
这是我的项目目录:
由于缺乏声誉,我无法发布图片,但我的控制器文件名是influencer_reports_controller.rb
影响者报告控制器.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
影响者报告模型.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
路线.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'
错误消息还有更多内容,但我觉得没有必要发布。
解决方案
您的控制器被调用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 知道控制器的正确外壳。
推荐阅读
- javascript - Recreating aniamtion / side scroll slideshow
- javascript - Dropzone 脚本(仅限于 dropzone 和放置时的初始位置)
- angular - 错误 TS2709:不能使用命名空间“路由器”作为 Angular 11.2 中的类型
- xcode - 没有这样的模块“GoogleMobileAds”,框架路径错误
- opencv - 使用无人机摄像头进行对象跟踪
- wix - WiX:根据属性值在 RegistryKey 元素中设置 Root 属性
- c++ - 如何生成知道其类型的类的字段名称?
- flutter - InputDecoration 中 Flutter App 中的文本溢出
- c - C字形打印:Unicode直接或从Unicode到XML的转换
- python - Python Selenium find_element_by_id 如果未找到,则默认为