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.
Related
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
I'm trying have a different frontend and backend view for certain parts of my app, starting with Users.
I'm getting the error uninitialized constant Office::DashboardController, which I understand means it can't find the controller? I feel like it's an issue with how I've setup the namespace / directories, but I can't seem to work it.
routes.rb
namespace :office do
root to: "dashboard#index"
resources :users
end
office/dashboards_controller.rb
class Office::DashboardsController < ApplicationController
layout "office"
end
office/users_controller.rb
class UsersController < Office::DashboardController
def index
#users = User.all
end
def show
#user = User.find_by_username(params[:id])
end
end
There is a chance I've completely messed this up and it's totally wrong...if that's the case, I would love some feedback!
Many thanks in advance :)
You have DashboardController inside Office module, but UsersController isn't in the module. Why is that? Both of them are in the same folder.
So, either
class Office::UsersController < Office::DashboardController
or
module Office
class UsersController < DashboardController
should work as expected.
Tip: you can run rails routes and see folders/class names Rails expects.
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.
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
I have the following setup:
class UsersController < ApplicationController
...
end
class Admin::BaseController < ApplicationController
...
end
class Admin::UsersController < Admin::BaseController
...
end
And likewise specs:
#spec/controllers/users_controller_spec.rb:
describe UsersController do
...
end
#spec/controllers/admin/users_controller_spec.rb
describe Admin::UsersController do
...
end
All the specs run fine when run independantly, however when I run all together I get the warning:
toplevel constant UsersController referenced by Admin::UsersController
And the specs from the admin controller don't pass.
Routes file:
...
resources :users
namespace "admin" do
resources :users
end
...
Rails 4, Rspec 2.14
Can I not use the same name for controllers in different namespaces?
This happens when a top level class get autoloaded before a namespaced one is used. If you have this code without any class preloaded :
UsersController
module AdminArea
UsersController
end
The first line will trigger constant missing hook : "ok, UsersController does not exist, so let's try to load it".
But then, reaching the second line, UsersController is indeed already defined, at top level. So, there's no const_missing hook triggered, and app will try to use the known constant.
To avoid that, explicitly require proper classes on top of your spec files :
#spec/controllers/users_controller_spec.rb:
require 'users_controller'
And
#spec/controllers/admin/users_controller_spec.rb
require 'admin/users_controller'