LoadError (Unable to autoload constant Api::V1::UsersController) - ruby-on-rails

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.

Related

Unable to autoload constant API Controller in Rails 4 model in module

I'm trying to create an api controller for a model defined in a module
Model class:
module Reports
class Report < ActiveRecord::Base
end
end
Api controller:
class API::V2::ReportsController < API::V2::BaseController
end
Route:
namespace :api, defaults: { format: :json } do
namespace :v2 do
resources :reports
end
end
The error I get when try to call api/v2/reports is:
LoadError (Unable to autoload constant Report, expected /.../app/models/reports/report.rb to define it):
Is there a way to solve this, making the api controller look for Reports::Report instead of Report?
Your controller needs to include Reports somehow:
module Api
module V2
module Reports
class Report < API::V2::BaseController
your controller actions
end
end
end
end
Apart from that, I think it will be confusing if you use Reports::Report?

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

Unable to autoload constant in API Controller

I'm trying to setup a RoR API but I'm hitting a few roadbump along the way. After tiresome coding to get the database setup right I'm hitting an error that doesn't quite make sense to me (I'm new to RoR)
Unable to autoload constant Api::V1::SubmissionsController, expected ./app/controllers/api/v1/submissions_controller.rb to define it
I'm not quite sure what this error means and my interwebs searches are coming up empty on a clear answer. I'm hoping any of you can help me here.
Routes
Rails.application.routes.draw do
namespace :api, :defaults => {:format => :json} do
namespace :v1 do
get "/submissions", to: "submissions#index"
end
end
end
submission.rb
class Submission < ActiveRecord::Base
end
submissions_controller.rb
class API::V1::SubmissionsController < ApplicationController
def index
#submissions = Submission.all
render json: #submissions, status: :ok
end
end
Your file contains:
class API::V1::SubmissionsController < ApplicationController
Rails expects:
class Api::V1::SubmissionsController < ApplicationController
What does your folder structure look like for app/controllers & app/views? Depending on how these files were generated, by "rails generate" or manually created, if you namespace, the folder structure has to match in controllers and views directories.
app/controllers/api/v1
app/views/api/v1

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

Rails3: Expected admin/items_controller.rb to define ItemsController

The following causes unexpected exception:
Expected app/controllers/admin/items_controller.rb
to define ItemsController
Foo::Application.routes.draw do
resources :items
match '/admin' => 'admin/overview#index', :as => :admin
namespace :admin do
resources :items
end
end
% cat app/controllers/admin/items_controller.rb
class Admin::ItemsController < Admin::BaseController
end
% cat app/controllers/admin/base_controller.rb
class Admin::BaseController < ActionController::Base
% cat app/controllers/items_controller.rb
class ItemsController < ApplicationController
end
It worked for me in the Rails 2.3.5.
What might be wrong the code? How can I fix that?
I can't reproduce the error this particular time, but I have run into it in the past. From memory, there are two things you can do:
Go to the Rails console (rails console in your application directory) and type in ItemsController. The error message, if any, should be useful.
One hackish fix that I've used is to load items_controller.rb before Rails start to autoload the controllers. You can either prepend the config.autoload_paths array with the path, or you can add a manual require to somewhere in your application.rb.

Resources