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
Related
I have little problem about my code I make controller/admin/moderators_controller.rb.
I got an error on compilator uninitialized constant Admin (NameError) in moderators_controller.rb.
In navigator I got this error:
superclass must be a Class (Module given)
navigator error
class Admin::ModeratorsController < ActionController
def index
end
end
This is my routes.rb file:
Rails.application.routes.draw do
namespace :admin do
resources :moderators, only: [:index]
end
end
You’ve accidentally made your controller inherit from ActionController (a module) instead of ActionController::Base (a class). You need to add ::Base to the end there.
If this is Rails 5, the common convention now is to have a ApplicationController class in your app/controllers folder, and have all controllers inherit from that (it’s just a class that inherits from ActionController::Base, but gives you a place to put common methods).
Rails 5
class Admin::ModeratorsController < ApplicationController
def index
end
end
Rails 4 or below
class Admin::ModeratorsController < ActionController::Base
def index
end
end
What is wrong with the way I named my routes?
Do I need to change my folder structure and my module name to
:car_registration
/app/controllers/car_registration/steps_controller.rb
I would prefer to use the formatting I have, if possible.
Routes
scope module: 'CarRegistration' do
resources :steps
end
Controller
/app/controllers/CarRegistration/steps_controller.rb
module CarRegistration
class StepsController < ApplicationController
include Wicked::Wizard
steps :step1, :step2, step3
def show
#form_object_model ||= form_object_model_for_step(step)
render_wizard
end
def update
#form_object_model = form_object_model_for_step(step)
render_wizard #form_object_model
end
private
def form_object_model_for_step(step)
"CarRegistration::#{step.camelize}".constantize.new
end
end
end
ERROR
'CarRegistration/steps' is not a supported controller name. This can
lead to potential routing problems. See
http://guides.rubyonrails.org/routing.html#specifying-a-controller-to-use
(ArgumentError)
It looks like you have at least two problems. You are using a folder structure like:
app/controllers/CarRegistration/steps_controller.rb
That is unconventional. Instead, it should be:
app/controllers/car_registration/steps_controller.rb
Then, your routes should look like:
scope module: :car_registration do
resources :steps
end
I have a Configurable model:
# /models/configuration.rb
class Configuration < ActiveRecord::Base
end
When I reference Configurable in my pages_controller, it works fine:
class PagesController < ApplicationController
def search
#description = Configuration.find_by_name('description') || nil
end
end
But when I reference it in my application_controller.rb, like so
class ApplicationController < ActionController::Base
def get_menu
#menu = Configuration.where(name: 'menu') || nil
end
end
I get the error undefined method 'where' for ActiveSupport::Configurable::Configuration:Class. How can I prevent my Configuration model and ActiveSupport::Configurable::Configuration:Class from colliding like this, or reference my Configuration model directly?
Thanks in advance!
You need to prefix it
::Configuration.where(name: 'menu')
Notice the :: before the class name. They force the interpreter to use the Configuration class in the main namespace, rather than one in the ActiveSupport namespace.
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
Playing with Rails and controller inheritance.
I've created a controller called AdminController, with a child class called admin_user_controller placed in /app/controllers/admin/admin_user_controller.rb
This is my routes.rb
namespace :admin do
resources :admin_user # Have the admin manage them here.
end
app/controllers/admin/admin_user_controller.rb
class AdminUserController < AdminController
def index
#users = User.all
end
end
app/controllers/admin_controller.rb
class AdminController < ApplicationController
end
I have a user model which I will want to edit with admin privileges.
When I try to connect to: http://localhost:3000/admin/admin_user/
I receive this error:
superclass mismatch for class AdminUserController
This error shows up if you define two times the same class with different superclasses. Maybe try grepping class AdminUserController in your code so you're sure you're not defining it two times. Chances are there is a conflict with a file generated by Rails.
To complete what #Intrepidd said, you can wrap your class inside a module, so that the AdminUserController class doesn't inherit twice from ApplicationController, so a simple workaround would be :
module Admin
class AdminUserController < AdminController
def index
#users = User.all
end
end
end
I fixed it by creating a "Dashboard" controller and an "index" def. I then edited my routes.rb thusly:
Rails.application.routes.draw do
namespace :admin do
get '', to: 'dashboard#index', as: '/'
resources :posts
end
end