I'm migrating the majority of my application to the admin namespace and while there are lots of guides related to this, I still can't manage. I've been primarily following this answer, along with any results Google brings up (they all tend to agree). Could somebody please tell me what I'm doing wrong so I don't lose any more sleep?
Here is the error message:
wrong argument type Module (expected Class)
app/controllers/application_controller.rb:1:in `<top (required)>'
app/controllers/admin/admin_controller.rb:1:in `<top (required)>'
app/controllers/admin/home_controller.rb:1:in `<top (required)>'
routes.rb
namespace :admin do
root :to => "home#index"
resources :users
end
admin/admin_controller.rb
class Admin::AdminController < ApplicationController
admin/home_controller.rb
class Admin::HomeController < Admin::AdminController
admin/users_controller.rb
class Admin::UsersController < Admin::AdminController
I'm mostly sure it's something simple to related to the module and controller interaction, so I haven't included any other code. However, I should have found the solution by now and please let me know if any additional code is required.
Thanks.
I encountered the reverse problem "wrong argument type Class (expected Module)" and it turned out there was a helper defined as a Class instead of a Module, so try searching for classes that are inadvertently defined as modules. Like a controller defined as a Module.
I'd suggest you rename Admin::AdminController to Admin::BaseController.
Maybe you have something defined as Admin constant?
Try a fresh app with the same structure then add pieces from the current one and see where it breaks (Not so great suggestion, huh?).
I use the same organization for admin as you pasted...
"wrong argument type Module (expected Class)"
This means you are defining a 'class' but that name is already defined as a 'module' somewhere else. Search for what that could be...
Can u follow the below code, Your controllers are fine, can you use the routes I have specified here.
class Admin::AdminController < ApplicationController
class Admin::UsersController < Admin::AdminController
This is same as what you have written, I think so.
namespace :admin do
resources :users do as_routes end
end
root :to => "home#index"
#Russell, I got that problem having created model AdminHelper (meant to contain admin help messages) :)
be careful in naming things!
I encountered such problem when I used paperclip's has_attached_file with invalid parameters.
Related
I'm running into the infamous No Method Error. I've worked my way through a number of examples here on STOF but I can't see an error in my code that stands out. I've checked that rake routes matches what I think should be happening and the paths provided from using resources in the routes.db file seem to be correct. I know I'm missing some small detail but I can't for the life of me see it now. Any help would be appreciated.
My Controller code:
class GenevarecordsController < ApplicationController
def index
#genevarecords = GenevaRecord.all.page(params[:page]).per(5)
end
def new
#genevarecord = GenevaRecord.new
end
end
My routes:
Rails.application.routes.draw do
root 'genevarecords#index'
resources :genevarecords
end
You have a naming discrepency between your model and your controller / routes.
your model is GenevaRecord, underscored makes it geneva_record. However your controller only has a single capital letter at the beginning: Geneverecords which underscored would be genevarecords. Therefore when you pass your model to the form it tries to use a controller / routes helpers with the same naming format as the model, which would be geneva_records_controller ie. GenevaRecordsController.
What you need to do is match your controller and routes to the same naming format as your model:
class GenevaRecordsController < ApplicationController
#...
end
Rails.application.routes.draw do
#...
resources :geneva_records
end
You need to take Did you mean? section seriously,
Anyway, if you closely look at ruby syntax following is the representation for the class name,
AbcDef and equivalent snake case is abc_def
In your case,
Your model is named as GenevaRecord but your controller is GenevarecordsController
change it to GenevaRecordsController, also you need to match it's equivalent snake case in routes...
Rails.application.routes.draw do
root 'geneva_records#index'
resources :geneva_records
end
So, when you pass #genevarecord to the form it is initialized as GenevaRecord.new and searches for geneva_records_path which is undefined because you have defined it as genevarecords_path which doesn't match you model (resources)..
Hope it helps in understanding..
So I wrote a little portfolio app in Rails 4. It has an admin namespace to allow me to only expose certain controllers to public.
Everything works great when in development, but when I deploy to a production env (or switch to production on my dev machine) I get the following error:
superclass mismatch for class WorksController (TypeError)
My controller structure looks like this:
controllers ->
admin ->
admin_controller.rb
portfolio ->
works_controller.rb
portfolio->
works_controller.rb
As you can see, I namespaced works inside of portfolio.
The controllers are declared as following:
/controllers/admin/admin_controller
class Admin::AdminController < ApplicationController
/controllers/admin/portfolio/works_controller
class Admin::Portfolio::WorksController < Admin::AdminController
/controllers/portfolio/works_controller.rb
class Portfolio::WorksController < ApplicationController
Now for my routes.rb, note I am using Friendly ID so I used a custom route to have a pretty client facing url.
get '/portfolio/:id' => 'portfolio/works#show', as: 'portfolio_work'
namespace :admin do
namespace :portfolio do
resources :works
end
end
So I know I must have screwed this up somehow, but I don't know how. The error I get is related to redeclaring a class, but I don't know how I managed to do so.
Any help is greatly appreciated.
Found this fix after #ReggieB tried to help me. Turns out having my Devise model 'Admin' and a namespace also called 'Admin' creates problems (as it should).
What happens if you redefine /controllers/admin/portfolio/works_controller line this:
module Admin
class Portfolio::WorksController < AdminController
Can I have controllers in Rails that are 3 levels deep inheritance? One would think such a trivial thing is possible, but the concrete controller at the "third" level gives the generic/useless error of "uninitialized constant Ns2::SecondController"
This is basically with this code (I haven't tried this exact code)
module Ns3
class ThirdController < Ns2::SecondController
end
end
module Ns2
class SecondController< Ns1::FirstController
end
end
module Ns1
class FirstController< ApplicationController
end
end
NOTE: The use of namespaces, within the routes and all such directories should be set up properly.
I'm sure I could rearrange the logic and get something working with mixins or helpers. However, I'd like the immediate question answered for my own benefit. Either Y/N or a way passed the error. Not interested in a refactoring work-around solution ATM. Though I guess it couldn't hurt.
Thanks
This can be done.
However it appears RoR is weird, and that you have to implicitly specify the namespace for base classes. If you let it default to the current namespace it acts weird.
Its most likely a typo in either the class name or filename.
You need to put the classes in the correct file/directory structure for Rails autoloading to work, e.g:
#/controllers/ns3/third_controller.rb
module Ns3
class ThirdController < Ns2::SecondController
end
end
#/controllers/ns2/second_controller.rb
module Ns2
class SecondController < Ns1::FirstController
end
end
#/controllers/ns1/first_controller.rb
module Ns1
class FirstController < ApplicationController
end
end
Another thing to try is scoping from the root namespace so with a :: prefix, like so:
module Ns1
class SecondController < ::Ns1::FirstController
end
end
You could also try this:
#/controllers/ns3/third_controller.rb
class Ns3::ThirdController < ::Ns2::SecondController
end
I'm having an error with my routes/resources and controllers.
I have the following in the routes.rb:
# routes.rb
resources :users do
resource :schedule
end
And I have a schedule_controller.rb inside controllers/users/ set up as I think it should be:
class Users::ScheduleController < ApplicationController
# Controller methods here...
end
Running a rake:routes shows
user_schedule POST /users/:user_id/schedule(.:format) schedules#create
new_user_schedule GET /users/:user_id/schedule/new(.:format) schedules#new
edit_user_schedule GET /users/:user_id/schedule/edit(.:format) schedules#edit
GET /users/:user_id/schedule(.:format) schedules#show
PUT /users/:user_id/schedule(.:format) schedules#update
However, navigating to /users/:user_id/schedule is returning the following error:
uninitialized constant SchedulesController
My only thoughts on what the problem could be are that is has something to do with nested resources or declaring a single resource and I'm going wrong somewhere.
I'm using the helper
new_user_schedule_path(current_user)
when linking to my 'new' view.
It should be SchedulesController, not Users::ScheduleController. Controllers should only be namespaced when the route is namespaced with namespace. Controller names should also always be plural.
What you're creating is a nested resource, not a namespaced one.
Is the namespacing of the SchedulesController intentional? i.e. do you really mean to do this?
class Users::SchedulesController < ApplicationController
Or are you only doing that because schedules are a "sub-thing" from users?
The reason I ask this is because typically within Rails, nested resource controllers aren't namespaced. You would only namespace a controller if you wanted to modify the controllers in a special way under a namespace. A common example of this would be having some controllers under an admin namespace, inheriting from a BaseController within that namespace that would restrict only admins from acessing those controllers.
Option 1
If you didn't intentionally namespace this controller, then you want to remove the Users:: prefix from your controller, and move it back to app/controllers/schedules_controller.rb, the helpers back to app/helpers/schedules_helper.rb and the views back to app/views/schedules. Perhaps you ran a generator which also generated a Users::Schedule model, which should also need to be renamed to Schedule and moved back to app/models/schedule.rb.
Option 2
If you did intentionally namespace this controller, then you want to do this in your routes:
namespace :users do
resources :schedules
end
Leave everything that's been generated as it should be.
In your routes.rb you need to specify the controller like this:
resources :users do
resource :schedules, controller: 'users/schedules'
end
replace resources :users to
namespace :users
Because your schedule controller is inside users folder.
class Users::ScheduleController < ApplicationController
# Controller methods here...
end
Everytime i get a warning:
app/controllers/agency/agencies_controller.rb:1: warning: toplevel constant ApplicationController referenced by Agency::ApplicationController
My agencies_controller.rb:
class Agency::AgenciesController < Agency::ApplicationController
def index
...
end
...
end
And Agency::ApplicationController:
class Agency::ApplicationController < ApplicationController
layout 'agency'
helper_method :current_agency
private
def current_agency
#current_agency ||= current_user.agency
end
end
What the rails wants from me? What is the trouble?
Same situation with another controller
class Agency::ClientsController < Agency::ApplicationController
...
end
And no warnings, no errors...
I realize this question is almost two years old but I recently stumbled upon this through another stackoverflow post and wanted to share some insight.
Basically, if your namespace Agency happens to be a class instead of a module, you'll get that warning. In the stackoverflow post I pasted above, they had a model (class) of Admin and their namespace was also Admin.
This provides a better explanation of what is happening.
So check to see if your code isn't defining an Agency class somewhere. Good luck.
I had similar issues running Spork and Watchr in my Admin namespaced controllers. So i've fixed this by adding following code into each_run block in spec_helper.rb:
Dir[File.expand_path("app/controllers/admin/*.rb")].each do |file|
require file
end
All credits goes to guy from this thread
ApplicationController is the name of the superclass controller that Rails generates for you when you create a new project that all your other controller classes inherit from. There's probably a conflict somewhere because you've used the same name, even though you put it within a namespace.
Try giving your Agency::ApplicationController a different name.
I had similar issues, after setting up Spork and Watchr. In the process, I turned off class cacheing (config_cache_classes => false in config/environments/test.rb) so that changes would be reloaded as necessary in the spork environment. Turning class cacheing back on made the warnings go away.
In my case it was the problem with Devise. I had a devise model Admin and a namespaced routes Admin. Changing the namespaced route to Admins solved the problem.
Solution for me was add this line:
# spec/rails_helper.rb
Dir[File.expand_path("app/controllers/admin/*.rb")].each { |file| require file }