Rails Controllers in subfolder ActionController::RoutingError (uninitialized constant - ruby-on-rails

I've seen related questions here in StackOverflow, but I still can't make it work.
I'm making an API with subfolders inside controllers, but I keep getting this error:
LoadError (Unable to autoload constant Api::Report::ReportController, expected ...
/controllers/api/report/report_controller.rb to define it):
Or this one:
ActionController::RoutingError (uninitialized constant Api::Report::ReportController):
This is my folder structure:
->controllers
->api
->report
infected_controller.rb
report_controller.rb
# inflected_controller.rb
module Api
class Report::InfectedController < ReportController
def infected
end
end
end
# report_controller.rb
module Api
class ReportController < ApplicationController
def index
end
end
end
And my routes.rb
Rails.application.routes.draw do
apipie
namespace :api do
scope module: 'report' do
get 'infected' => 'infected#infected'
resources :report
end
end
end

module Api
module Report # <============== missing module
class ReportController < ApplicationController
def index
end
end
end
end
Also
module Api
class Report::InfectedController < Report::ReportController
def infected
end
end
end

Try below code
# report_controller.rb
module Api
class Report::ReportController < ApplicationController
def index
end
end
end

Related

Route not find - Rails

Rails 3.2
In my controllers/admin/accounts_receivables_contoller.rb, I have:
class Admin::AccountsReceivables < Admin::ApplicationController
def index
...
end
and in one of the views, I have:
= link_to admin_accounts_receivables_path
In my config/routes.rb, I have:
namespace :admin do
resources :accounts_receivables do
collection do
get 'admin_report'
get 'customer_report'
post 'process_invoices'
end
end
end
rake routes, produces:
admin_accounts_receivables GET admin/accounts_receivables(.:format) admin/accounts_receivables#index
However, when I click on the link, I get (in the browser, but no entry in the log file):
uninitialized constant Admin::AccountsReceivablesController
I do not have a corresponding AccountsReceivable model, as I don't need it.
Any ideas?
The class should be named AccountsReceivablesController and you should nest the class explicitly instead of using the scope resolution operator so that it has the correct module nesting:
module Admin
class AccountsReceivablesController < ApplicationController
def index
# ...
end
end
end
When you use the scope resolution operator class Admin::AccountsReceivablesController - the module nesting is resolved to the point of definition which is Main (the global scope) and not Admin. For example:
module Admin
FOO = "this is what we expected"
end
FOO = "but this is what we will actually get"
class Admin::AccountsReceivablesController < Admin::ApplicationController
def index
render plain: FOO
end
end
See The Ruby Style Guide - namespaces.
class Admin::AccountsReceivables < Admin::ApplicationController
should be...
class Admin::AccountsReceivablesController < Admin::ApplicationController

superclass mismatch for class usercontroller

I have created API in rails 5.
I am getting error superclass mismatch for class UsersController.
My controller:
module Api
module V1
class UsersController < ApplicationController
def index
users = User.Order('created_at DESC')
render json: {status:"SUCCESS", message:"Load Users", data:users}, status: :ok
end
def create
user = User.new(user_params)
end
def user_params
params.permit(:firstname, :lastname, :email, :age, :id)
end
end
end
end
My routes:
Rails.application.routes.draw do
namespace :api do
namespace :v1 do
resources :users
end
end
end
Here is my folder structure:
I got below error in console:
Actually, I have just started in ruby on rails. I tried to figure out problem but couldn't find it.
You need to reference ApplicationController from the Main module (the "global" namespace in Ruby):
module Api
module V1
class UsersController < ::ApplicationController
:: tells Ruby to resolve the constant from main rather than the current module nesting which is Api::V1.
You also need to ensure that ApplicationController inherits from ActionController::API.
See:
Everything you ever wanted to know about constant lookup in Ruby

Rails namespaced controller uninitialized constant error

Similar to this question but the answers there don't work here.
In routes.rb
scope '/api/ do
namespace :v1 do
scope :reports do
get '/reportXYZ', to: 'reports#reportXYZ'
end
end
end
In app/controllers/V1/reports_controller.rb
module V1
class ReportsController < ApplicationController
def reportXYZ
...
end
end
end
the error:
uninitialized constant V1 on Windows only, works fine on ubuntu. How come?
In your routes .rb file instead of writing scope for api write like this
namespace :api do
namespace :v1 do
scope :reports do
get '/reportXYZ', to: 'reports#reportXYZ
end
end
end
And in your controller
module api
module V1
class ReportsController < ApplicationController
def reportXYZ
...
end
end
end
END

Understanding Namespacing

When I run my tests I get an error undefined constant Admin::Blog::Category.
The model is defined like so:
module Blog
class Category < ActiveRecord::Base
...
end
end
When i try to call it from my admin controller I get the error. The controller is defined like so:
module Admin
module Blog
class CategoriesController < Admin::AdminController
...
end
end
end
My question is this name-spaced incorrectly or how do I call the Blog::Category model?
You need to call it as ::Blog::Category.all, for e.g. to get all categories inside the controllers. A litle insight how the search will be happening.
module Blog
class Category
#...
end
end
module Admin
module Blog
class Categories
def self.class_name
Module.nesting
end
end
end
end
Admin::Blog::Categories.class_name
# >> [Admin::Blog::Categories, Admin::Blog, Admin]
After this it will search on the toplevel. Let's see 2 examples:
module Blog
class Category
#...
end
end
Category = 12
module Admin
module Blog
class Categories
def self.const_value
Category
end
end
end
end
Admin::Blog::Categories.const_value # => 12
and, now...
module Blog
class Category
#...
end
end
Category = 12
module Admin
Category = 11
module Blog
class Categories
def self.const_value
Category
end
end
end
end
Admin::Blog::Categories.const_value # => 11
Following 2 examples above, this example ofcourse, should throw error :
module Blog
class Category
#...
end
end
module Admin
module Blog
class Categories
def self.const_value
Category
end
end
end
end
Admin::Blog::Categories.const_value
# uninitialized constant Admin::Blog::Categories::Category (NameError)
Read this guide tutorial to understand this topic.
Last to finish:
module Blog
class Category
end
end
module Admin
module Blog
class Categories
def self.const_value
::Blog::Category
end
end
end
end
Admin::Blog::Categories.const_value # => Blog::Category

Rails Controller in nested module cannot resolve model in module with the same

I have a rails model located at app/models/scheduling/availability.rb which looks like:
class Scheduling::Availability < ActiveRecord::Base
end
I have a Rails controller located at *app/controllers/admin/scheduling/availabilities_controller.rb* which looks like:
class Admin::Scheduling::AvailabilitiesController < ApplicationController
def index
#availabilities = Scheduling::Availability.all
end
end
My routes look like:
namespace :admin do
namespace :scheduling do
resources :availabilities
end
end
When trying to load the url:
/admin/scheduling/availabilities
I get the error:
uninitialized constant
Admin::Scheduling::AvailabilitiesController::Scheduling
I have a feeling this is because Rails is confusing the Scheduling module/namespaces.
What am I doing wrong?
Found my answer in another answer.
Need to preface my module with ::
class Admin::Scheduling::AvailabilitiesController < ApplicationController
def index
#availabilities = ::Scheduling::Availability.all
end
end

Resources