Reuse Devise authentication for custom controller - ruby-on-rails

I am trying to create a custom controller in the admin section of Spree and reuse the devise authentication mechanism. How do I go about doing this. I have simply tried to do the following:
module Spree
module Admin
class WorkflowController < Spree::Admin::BaseController
end
end
end
And I created a route like this:
namespace :admin do
resources :workflow, :only => [:index, :show]
end
I am getting the following error:
ActionController::RoutingError (uninitialized constant Admin):
So, any thoughts on how to best create a custom controller or am I just doing something wrong with this?

This is happening because your controller is nested inside the Spree namespace, but your routes are not. If you want to extend Spree's routes, then do this:
Spree::Core::Engine.routes.draw do
namespace :admin do
resources :workflow, :only => [:index, :show]
end
end

Related

Rails 5 API mode: can't reach show action for a nested resource

I'm using rails 5.2.1 in API mode with 2.5.3.
I have the following routes.rb file..
Rails.application.routes.draw do
namespace :api do
resource :groups, only: [:show]
end
end
.. and the following app/controllers/api/groups_controller.rb file
class Api::GroupsController < ApplicationController
def show
binding.pry
end
end
The following request http://localhost:3000/api/groups arrives in the controller's action properly, triggering the binding.pry.
The issue is that the following request http://localhost:3000/api/groups/1 thraws a Routing error:
No route matches [GET] "/api/groups/1"
Why is that happening ?
Since you want to pass the id to show action, I guess probably you need this in your routes:
Rails.application.routes.draw do
namespace :api do
resources :groups, only: [:index, :show]
end
end
and have index and show actions in your Api::GroupsController.
I suggest you go through rails guides. Understand the difference between resource and resources

Ruby on Rails add nested controller/resource

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

Using Devise Gem with namespaces throwing ActionController::RoutingError

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.

Route a controller to namespace :admin to /admin

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

How do you render a rails partial that is outside of a namespace in a view that is inside of a namespace?

I've got a 'static' controller and static views that are pages that don't utilize ruby in their views. For these pages, I have a sitemap partial that is generated programatically and used in the application layout file. Namespaced routes still use the application layout file but are taking the static routes and trying to namespace them too.
Here's the relevant portion of the route file:
namespace :admin do
resources :verse_categories
resources :verses
resources :songs
resources :flowers
resources :visits, :except => [:new, :create]
end
match ':action' => 'static'
root :to => 'static#home'
Here's the error I'm getting:
No route matches {:controller=>"admin/static", :action=>"about"}
Note that about is one of the static pages that the sitemap partial uses.
So, how can I resolve this routing issue so that it's not trying to find my static sites inside of the admin namespace? Any help would be appreciated!
What about:
namespace :admin do
...
get "/about" => "static#about"
end
Or
scope "/admin" do
get "/about" => "static#about"
end
This gist explains how to add directories to the search path for namespaced directories. I ended up doing the following:
class Static::BaseController < ApplicationController
def self._prefixes
super | ["other_directory"]
end
end

Resources