Rails namespaced controller uninitialized constant error - ruby-on-rails

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

Related

LoadError (Unable to autoload constant Api::V1::UsersController)

I'm using Rails 5 and I'm getting this error when calling a method in my Api. The thing is, the error only happens sometimes.
LoadError (Unable to autoload constant Api::V1::UsersController, expected /home/user/projects/project-name/app/controllers/api/v1/users_controller.rb to define it):
Relevant part of routes.rb:
Rails.application.routes.draw do
namespace :api do
namespace :v1 do
resources :users
end
end
end
Relevant part of the controller:
class Api::V1::UsersController < ApplicationController
I'm clueless of whats wrong, google a lot but couldn't find a solution.
Have you tried adding to the top of your controller
module Api
module V1
class className < ApplicationController
....
end
end
end
This should follow your directory structure.

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 Controllers in subfolder ActionController::RoutingError (uninitialized constant

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

Ruby on Rails rooting error

Im trying to create simple Ruby on Rails REST API.
app/controllers/api/vi/product_controller.rb
module Api
module V1
class ProductController < ApplicationController::API
def index
render json: {message: 'Welcome!'}
end
end
end
end
config/routes.rb
Rails.application.routes.draw do
namespace :api do
namespace :v1 do
get '/product', to: 'product_controller#index', as: 'product'
end
end
end
When I run project on localhost, I get uninitialized constant Api::V1::ApplicationController routing error. Can anyone help to such Ruby on Rails newbie as I am?
you just need to create a folder inside controllers called api and a v1 folder inside api.
You should provide all the controllers inside v1 folder.
In your app/controllers/api/v1/product_controller.rb
class Api::V1::ProductController < ApplicationController
def index
render json: {message: 'Welcome!'}
end
end
In your routes:
Rails.application.routes.draw do
namespace :api do
namespace :v1 do
get '/product', to: 'product_controller#index', as: 'product'
end
end
end
you nested the route, so it should be '/api/v1/product`
if you run rake routes from your console, you will get a list of all available routes.
for more information about routing and nested routes, have a look at rails guides
change this and try:
module Api
module V1
class ProductController < ApplicationController
def index
render json: {message: 'Welcome!'}
end
end
end
end

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