UsersController inside of namespace - ruby-on-rails

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.

Related

uninitialized constant Admin::AdminController

Getting an "uninitialized constant" error. I am trying to make a "AdminCotroller"(subcontroller) to the ApplicationController to control one area of the website (the "admin" area).
The routing seems to be correctly set up. I attach 2 things (both from: app/controller/admin/ folder) which produce the error:
1) the "parent" controller
class Admin::AdminController < ApplicationController
layout "admin/layout"
end
2) the "child" controller
class Admin::ProductsController < Admin::AdminController
PS: I wanted to make a separate layout and this was the only solution I could think off.
PPS: Folder Structure
Check the above file structure, that's what I have used.
And controllers as below:
class Admin::BaseController < ApplicationController
layout 'admin'
...
end
class Admin::AdminUsersController < Admin::BaseController
...
end
I don't know what I messed up those days. I haven't tried a given answer but instead I found a workable solution for naming I would like to share:
module Admin
class UsersController < BaseController
...
end
end
this way you achieve a namespace in a folder "admin" too.

Rails: not a supported controller name

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

Rails: SuperClass Mismatch

I'm having trouble starting to build my own admin section. I get this error when trying to view example.com/admin:
TypeError in Admin::AdminController#dashboard
"superclass mismatch for class AdminController"
My admin controller is in app/controllers/admin/admin_controller.rb
Here is my routes.rb:
Rails.application.routes.draw do
namespace :admin do
root :to => "admin#dashboard"
resources :posts
end
Here is my AdminController:
class AdminController < ApplicationController
def dashboard
print "Dashboard"
end
end
My plan is to have example.com/admin go to the admin dashboard. To edit/create posts: /admin/posts.
You already have a Admin::AdminController class defined elsewhere. Which inherits from different class other then ApplicationController
If you have not created a second Admin::AdminController class yourself, it is likely one of your Gems or plugins already defines it.
If you are using active_admin or rails_admin gem , maybe it does have class with the above name AdminController
You can cross check by replacing the AdminController with some other name maybe AdminController2

The correct way to do inheritance in Rails mountable engine

Today, I found a very strange issue while writing a mountable engine using Rails 3.
I have the following ApplicationController in my engine:
module Marketplace
class ApplicationController < ::ApplicationController
before_filter :merge_abilities
layout 'marketplace/application'
def marketplace_current_user
Marketplace::User.find(current_user.id)
end
private
def merge_abilities
current_ability.merge(Ability.new(current_user))
end
end
end
And my User model definition is
module Marketplace
class User < ::User
devise omniauth_providers: [:facebook, :paypal]
has_one :payout_identity, class_name: "Marketplace::PayoutIdentity"
has_many :course_purchases, class_name: "Marketplace::CoursePurchase"
def has_verified_payout_identity?
self.payout_identity and self.payout_identity.receiver_id
end
end
end
After starting up the rails server, the first request to load a page will have the controller run the marketplace_current_user method correctly and load the engine's User class. However any request after the first one will given a strange NameError - "uninitialized constant Marketplace::Marketplace::User".
I tried removing the namespace in marketplace_current_user definition but it will load the main app's User class instead.
At last when I change my ApplicationController to look like this:
class Marketplace::ApplicationController < ::ApplicationController
before_filter :merge_abilities
layout 'marketplace/application'
def marketplace_current_user
Marketplace::User.find(current_user.id)
end
private
def merge_abilities
current_ability.merge(Ability.new(current_user))
end
end
Everything would work fine.
Could someone enlighten me where I got it wrong at the beginning? Was it wrong to do inheritance that way?

Rails - superclass mismatch

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

Resources