I added an Admin namespace to my app so when logging in to the administration area, it would have to be like this: admin/websites and admin/page/8
So this is what I have in my routes.rb
namespace :admin do |admin|
match '/' => 'dashboard#index'
resources :websites
resources :pages
resources :sessions
get 'login' => 'sessions#new', :as => 'login'
get 'logout' => 'sessions#destroy', :as => 'logout'
end
I have admin_controller.rb in app/controllers directory.
class Admin::BaseController < ApplicationController
protect_from_forgery
include UrlHelper
...
I created an admin directory inside app/controllers. So I have this inside app/controllers/admin/websites_controller.rb
class Admin::WebsitesController < ApplicationController
Some other answers suggested class Admin::WebsitesController < Admin::BaseController, but that never worked for me. If I'm wrong please let me know.
So then in my layout file (app/views/layouts/application.html.erb) I have links like this one edit_admin_website_path(#website) that give me routing errors Routing Error No route matches {:action=>"edit", :controller=>"admin/websites"} Whyyyy?! :(
Add a file named application_controller.rb in the admin directory with this content:
class Admin::ApplicationController < ApplicationController
end
Then, for each controller on this directory, extend the Admin::ApplicationController class.
Did you try this?
admin_edit_website_path(#website)
Rails namespaces rely on folder structure for loading the right classes. You should structure it like this:
app/controllers
admin_controller.rb # class AdminController < ApplicationController
app/controllers/admin
websites_controller.rb # class Admin::WebsitesController < AdminController
The AdminController should be defined outside the admin folder. If put it in there you'd have to refer to it as Admin::AdminController which is a little odd. In fact, you could call it AdminNamespaceController to be clear.
You can also use rails generate which will set things up for you in the expected places, although I don't think it creates the namespace base class for you to inherit from.
Related
I have an application that I want to have a route that is /admin/active_vulnerabilities but when I generate the controller as rails generate controller ActiveVulnerabilities and put the following in my routes.rb
namespace :admin do
resources :users
resources :active_vulnerabilities
# Admin root
root to: 'application#index'
end
But I get the error uninitialized constant Admin::ActiveVulnerabilitiesController so I changed my controller to class Admin::ActiveVulnerabilitiesController < ApplicationController
I then get the error Unable to autoload constant ActiveVulnerabilitiesController, expected /home/luke/projects/vuln_frontend/app/controllers/active_vulnerabilities_controller.rb to define it but the file mentioned is my controller named exactly as that.
Your controller should be put in app/controllers/admin/ because the namespace. Otherwise, you can forget this directory and the namespace and use just scope
scope :admin do
resources :active_vulnerabilities
end
class ActiveVulnerabilitiesController < ApplicationController
I am somewhat new to RoR,
I want to have a structured directory, because project may become big I don't want to have all controllers directly into controllers directory.
I would want something as
app/
controllers/
application_controller.rb
groupa/
athing_controller.rb
athing2_controller.rb
groupb/
bthing_controller.rb
However when I place in the routes.rb the following:
get 'athing', :to => "groupa/athing#index"
I get the following error on localhost:3000/athing/ :
superclass mismatch for class AthingController
Which is like:
class AthingController < ApplicationController
def index
end
end
Am I missing something?
Can I place subdirectories at all?
Try to use namespace instead:
In your routes:
namespace :groupa do
get 'athing', :to => "athing#index"
end
In your controller:
class Groupa::AthingController < ApplicationController
In browser:
localhost:3000/groupa/athing/
Modularity
When you put your controllers (classes) into a subdirectory, Ruby/Rails expects it to subclass from the parent (module):
#app/controllers/group_a/a_thing_controller.rb
class GroupA::AThingController < ApplicationController
def index
end
end
#config/routes.rb
get :a_thing, to: "group_a/a_thing#index" #-> url.com/a_thing
I've changed your model / dir names to conform to Ruby snake_case convention:
Use snake_case for naming directories, e.g. lib/hello_world/hello_world.rb
Use CamelCase for classes and modules, e.g class GroupA
Rails routing has the namespace directive to help:
#config/routes.rb
namespace :group_a do
resources :a_thing, only: :index #-> url.com/group_a/a_thing
end
... also the module directive:
#config/routes.rb
resources :a_thing, only: :index, module: :group_a #-> url.com/a_thing
scope module: :group_a do
resources :a_thing, only: :index #-> url.com/a_thing
end
The difference is that namespace creates a subdir in your routes, module just sends the path to the subdir-ed controller.
Both of the above require the GroupA:: superclass on your subdirectory controllers.
In config/routes.rb
namespace :namespace_name do
resources : resource_name
end
In app/controllers/
create a module name with your namespace_name, in that place your controllers
In that controller class name should be like
class namespace_name::ExampleController < ApplicationController
I have a class called Device. It has a model device.rb
I have set the routing up so that the same controller is called from two different paths. i.e. the paths:
/driver_api/v1/devices
and
/sender_api/v1/devices
both call the following controller:
/user_api/v1/devices
In my routes.rb I have:
namespace :driver_api do
namespace :v1 do
resources :devices, :only => [:create], controller: '/user_api/v1/devices'
end
end
namespace :sender_api do
namespace :v1 do
resources :devices, :only => [:create], controller: '/user_api/v1/devices'
end
end
Now, in my devices controller, I'm trying to call a Device class method. i.e. in my controller:
class UserApi::V1::DevicesController < ApplicationController
Devise.method_name(input)
end
But i get an error:
uninitialized constant UserApi::V1::DevicesController::Device
Why am I getting this error?
Because you wrote Devise and not Device that could be a good reason.
If that is just a typpo from the question, here's another alternative.
Sometimes there's some kind of naming problem when the classes are defined in the way you did (I ignore the reason why that happens)
Try to decompose the class scoping by defining the modules:
module UserApi
module V1
class DevicesController < ApplicationController
# rest
end
end
end
Ok, I've seen a ton of different answers for how to get Devise working when using namespaces in your app, but none of them are working for me.
I have my app split up into three namespaces
Home (the public landing pages)
Account (the logged in profile/account of the user)
Admin (the admin backend which isn't written yet)
I've also split up all the partials into a base folder in each namespace. So each of my controllers inherit from the BaseController which inherits from the ApplicationController:
module Account
class UsersController < BaseController
end
end
And I created a sessions_controller.rb in both account and home that inherits from the devise sessions controller like this:
module Account
class SessionsController < BaseController < Devise::SessionsController
end
end
The goal is to have a login/ registration form in the Home namespace that lets users login to the users controller that is in the account namespace.
Right now when I click on the link generated by:
<%= link_to "register", new_user_registration_path %>
I'm getting
ActionController::RoutingError at /users/sign_up
uninitialized constant Account::RegistrationsController
My routes.rb file looks like this:
scope :module => "account" do
devise_for :users, :controllers => { :sessions => "account/sessions" }
resource :users
end
scope :module => "home" do
resources :home, :about, :jobs, :terms, :privacy, :android_availability, :about, :contact
end
get "home/index"
root :to => 'home::home#index'
end
The home controllers use a home layout and the account controllers use the application layout. I specify layout "home" in the home controllers, but I don't specify layout "application" in the account controllers because application is the default layout rails looks for.
Ok. I think I've covered all my bases. Any idea what I'm doing wrong here?
Thanks!
EDIT:
Ok, I've added a registrations_controller.rb file to the account namespace in the same way as the sessions_controller.rb file described above.
I also updated the routes.rb file:
scope :module => "account" do
devise_for :users, :controllers => {
:sessions => "account/sessions",
:registrations => "account/registrations" }
resource :users
end
Now I'm getting a new error that I don't understand. Here it is:
NoMethodError at /users/sign_up
undefined method `action' for Account::RegistrationsController:Class
It says the undefined method 'action' is in (gem) actionpack-3.2.11/lib/action_dispatch/routing/route_set.rb which doesn't make any sense.
Specifically is says the problem is here:
def dispatch(controller, action, env)
controller.action(action).call(env)
end
EDIT 2:
Here is the code from my registrations_controller.rb
module Account
class RegistrationsController < BaseController < Devise::RegistrationsController
end
end
EDIT 3:
module Account
class BaseController < ApplicationController
end
end
Ok, the above is my base_controller.rb, which just inherits from the ApplicationController. All the other controllers inherit from BaseController. Because I've split my app into three namespaces, the base_controller is there to tell the other controllers in the namespace that the partials are in a folder named base within their namespace. As shown in this RailsCast
I get a missing partial error if I don't incude the BaseController because the devise controllers can't find the partials.
Read your errors! :-)
For starters, looks like you need to define an Account::RegistrationsController, the same way you did your Account::SessionsController.
I feel like this may be a dumb question, but it's late and my head is melting a bit.. So I appreciate the assistance.
I'm trying to map the url http://localhost:3000/admin to a dashboard controller but i'm epically failing. Maybe this isn't even possible or the completely wrong idea but anyway my routes looks like this and yes
namespace :admin do
resources :dashboard, { :only => [:index], :path => '' }
...
end
and my simple dashboard_controller.rb
class Admin::DashboardController < ApplicationController
before_filter :authenticate_user!
filter_access_to :all
def index
#schools = School.all
end
end
and my view is located in views/admin/dashboard/index.html.erb
thanks for any input
If all you're trying to do is route /admin to that dashboard controller, then you're overcomplicating it by namespacing it like that.
Namespacing with a nested resource like that would mean that it would be /admin/dashboards for the :index action instead of having a clean /admin route (and you can verify that by running rake routes at the command line to get a list of your routes).
Option 1: You meant to namespace it like that
# putting this matched route above the namespace will cause Rails to
# match it first since routes higher up in the routes.rb file are matched first
match :admin, :to => 'admin/dashboards#index'
namespace :admin do
# put the rest of your namespaced resources here
...
end
Option 2: You didn't mean to namespace it like that
Route:
match :admin, :to => 'dashboards#index'
Controller:
# Remove the namespace from the controller
class DashboardController < ApplicationController
...
end
Views should be moved back to:
views/dashboards/index.html.erb
More info: http://guides.rubyonrails.org/routing.html
Regarding to http://guides.rubyonrails.org/routing.html I prefer this
namespace :admin do
root to: "admin/dashboards#index"
resources :dashboard
end
Try this:
namespace :admin do
root to: 'users#index' # whatever. Just don't start with /admin
#resources :dashboards <= REMOVE THIS LINE !
end