Rails Controller Inheritance Routing Error - ruby-on-rails

I am trying to define a namespace as admin and my routes file is the following;
namespace :admin do
root 'base#index'
resources :boats
end
So I have admin folder, and inside I have base_controller.rb;
class Admin::BaseController < ApplicationController
before_action :logged_in_user, :auth_admin
def index
end
end
and I created cars_controller.rb inherited from BaseController;
class Admin::CarsController < BaseController
def index
#cars = Car.all
end
end
So, like this I get error from the console;
ActionController::RoutingError (uninitialized constant BaseController):
app/controllers/admin/cars_controller.rb:1:in `<top (required)>'
If I change instead of BaseController to ApplicationController, it works without a problem. Bur I could not figured it out why it gives such error.

ActionController::RoutingError (uninitialized constant
BaseController):
Your BaseController is inherited from Admin, so you need to write Admin::BaseController instead of BaseController
class Admin::CarsController < Admin::BaseController

Related

Uninitialized constant Admin (NameError)

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

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

NameError in PagesController#index

I'm trying to create a login system with devise, cancancan and rolify. The devise part is working. I recently added cancancan and rolify and I'm trying to test if they are working.
My cancancan ability file:
class Ability
include CanCan::Ability
def initialize(user)
if user.has_role? :admin
can [:index], Page
else
can [:index,:lecture]
end
end
end
I have a root page root to: "pages#index" which is working. If I add load_and_authorize_resource at the top of the pages controller like
class PagesController < ApplicationController
load_and_authorize_resource
def index
end
def lecture
end
end
I get back when trying to access it:
NameError in PagesController#index generate by these lines:
names.inject(Object) do |constant, name|
if constant == Object
* constant.const_get(name)
else
candidate = constant.const_get(name)
next candidate if constant.const_defined?(name, false)
The * is the line that gives the error. I also saw in the rolify documentation that I have to add resourcify to the files that are going to make use of it. But if I try to add it I get an error undefined method resourcify. How can I solve this ?
Check the following:
GemFile:
gem 'load_and_authorize_resource'
# bundle install after
ApplicationController:
class ApplicationController < ActionController::Base
include LoadAndAuthorizeResource
end
PagesController
class PagesController < ApplicationController
load_and_authorize_resource
....
More Info here Load And Authorize Resource

uninitialized constant of controller

Hi i am trying to use cancan but I have very irritating error, namely: uninitialized constant Edition
for controller :
class EditionsController < ApplicationController
before_filter :authenticate_user! #devise
load_and_authorize_resource
def index
end
end
with this route:
get "editions/index"
and such abilities:
user ||= User.new # guest user (not logged in)
if user.has_role? "admin"
can :manage, Edition
cannot :commission
else
can :read, :commission
end
And additional question, how i can create cancan ability for singular(name) controller ?
for example PhotoController
The problem is that you are using a custom class.
CanCan tries to singularize the controller class name to match the model class name and that sometimes isn't what we expect:
...
If the model class is namespaced differently than the controller you will need to specify the :class option.
source: https://github.com/ryanb/cancan/wiki/authorizing-controller-actions
In my case, I had a class with no model, so I just added ":class => false" at the end of the command:
load_and_authorize_resource :class => false
If you have load_and_authorize_resource defined in the ApplicationController and want to override it only for this controller:
class EditionsController < ApplicationController
skip_load_and_authorize_resource
def index
#editions = Edition.accessible_by(current_ability).order(:date)
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