HBaig
HBaig

Reputation: 23

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

I'm working on an API that takes specific data, creates a pdf and sends it in an email using rails 5. After setting up my routes/controllers/model I keep getting ActionController::RoutingError (uninitialized constant InfluencerreportsController).

I've surfed the web trying to find the solution, and while there are many others with the same error, none of their solutions worked out for me. As far as I could tell, the error means my file names and the controller don't match, but there are no typos anywhere. I'm aware of the error message saying InfluencerreportsController but I tried changing my controller to that name and that did nothing. Would I possibly have to change it in other locations also?

Another thing worth mentioning is that I used rails generate for everything (contollers, models, and migrations), so I didn't create any of the files myself.

This is my first time dealing with nested_attributes, so I'm not sure if I did it correctly or if that could be causing the problem.

Thank you in advance!

Here is my project directory:

I'm not able to post images because of a lack of reputation, but my controller filename is 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

error message:

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'

There's more to the error message, but I felt that wasn't necessary to post up.

Upvotes: 1

Views: 3010

Answers (1)

Mark
Mark

Reputation: 6455

Your controller is called InfluencerReportsController, and your error message says the app is looking for a InfluencerreportsController controller. Try changing your routes to:

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

This should let rails know the correct casing for the controller.

Upvotes: 2

Related Questions