Rails 4 Devise custom routes - ruby-on-rails

I am using 'devise', '~> 3.4.1' and rails 4 in my app. When I try to create custom routes am getting wiered error. What else I need to add in my route.rb
Error:
ArgumentError: ' devise/sessions' is not a supported controller name.
This can lead to potential routing problems.
See
routes.rb
Rails.application.routes.draw do
devise_for :users
devise_scope :user do
get 'register', to: 'devise/registrations#new', as: :register
get 'login', to: ' devise/sessions#new', as: :login
end
The problem occured at login route.

There is a typo in your code, a space before 'devise/sessions#new'.
This,
get 'login', to: ' devise/sessions#new', as: :login
should be
get 'login', to: 'devise/sessions#new', as: :login

Try out this code:
devise_scope :user do
get '/login' => 'devise/sessions#new'
get '/register' => 'devise/registrations#new'
end

You need to do it this
devise_for :users, path_names: {
sign_in: 'login', sign_out: 'logout',
password: 'secret', confirmation: 'verification',
registration: 'register', edit: 'edit/profile'
}
Source:
http://www.rubydoc.info/github/plataformatec/devise/ActionDispatch/Routing/Mapper:devise_for

devise_for :user, :path => ' ', :path_names => { :sign_in => "login" :sign_up => "register" }

Related

Change default routes Devise Rails

I have this routes.rb:
devise_for :users, :path => '', path_names:
{ sign_in: "login", sign_out: "logout", sign_up: "registration"}
I changed sign_in and sign_up routes and if you go to sign_up you will get 404 error, instead /registration will work. What I want is to change and add other routes like forgotten password in the same way.
If I type in the console rake routes, I see this for forgotten password:
new_user_password GET /password/new(.:format) devise/passwords#new
How can add the additional routes in a way that my custom defined route will work, but not the default?
Be sure to checkout the ActionDispatch::Routing::Mapper#devise_for documentation here.
You can just simply do something like this-
devise_for :users, path: 'auth', path_names: { sign_in: 'login',
sign_out: 'logout',
password: 'secret',
confirmation: 'verification',
unlock: 'unblock',
registration: 'register',
sign_up: 'cmon_let_me_in' }
Here is an example for the sessions, registrations, and passwords controller actions/routes:
devise_for :users, skip: [:sessions, :registrations, :passwords]
devise_scope :user do
# sessions
get 'login', to: 'devise/sessions#new', as: :new_user_session
post 'login', to: 'devise/sessions#create', as: :user_session
delete 'logout', to: 'devise/sessions#destroy', as: :destroy_user_session
# registrations
put '/account', to: 'devise/registrations#update'
delete '/account', to: 'devise/registrations#destroy'
post '/account', to: 'devise/registrations#create'
get '/register', to: 'devise/registrations#new', as: :new_user_registration
get '/account', to: 'devise/registrations#edit', as: :edit_user_registration
patch '/account', to: 'devise/registrations#update', as: :user_registration
get '/account/cancel', to: 'devise/registrations#cancel', as: :cancel_user_registration# passwords
# passwords
get 'new-pass', to: 'devise/passwords#new', as: :new_user_password
get 'edit-pass', to: 'devise/passwords#edit', as: :edit_user_password
patch 'edit-pass', to: 'devise/passwords#update', as: :user_password
post 'new-pass', to: 'devise/passwords#create', as: :user_password
end
As seen in the 4th code sample block in this wiki
You need to skip passwords and rebuild its routes as you want,
devise_for :users, skip: [:passwords]
devise_scope :user do
match '/forgotten-password' => 'devise/passwords#create', as: :user_password, via: [:post]
match '/forgotten-password' => 'devise/passwords#update', via: [:put, :patch]
get 'forgotten-password', to: 'devise/passwords#new', as: :new_user_password
end
Your custom routes will work. And it skip all routes in that modules.
NOTE : You need to overwrite all remaining routes of that module as you want.

Devise : overriding the devise routes and controllers at the same time

i want to ovverride my devise routes and the sessions controller from this gem at the same time. How do i do this ?
I thought about :
devise_for :admins, :skip => [:sessions],
controllers: { sessions: "admins/sessions" }
devise_scope :admin do
get 'login' => 'devise/sessions#new', :as => :new_admin_session
post 'login' => 'devise/sessions#create', :as => :admin_session
delete 'logout' => 'devise/sessions#destroy', :as => :destroy_admin_session
end
but my paths are chaging but the controller - not. How can I implement this right ?
When you specify controllers: { sessions: "admins/sessions" }, that implies that you have a file called sessions_controller.rb in this path: app/controllers/admins/sessions_controller.rb, and that it starts with:
module Admins
class SessionsController < Devise::SessionsController
If this is the controller you want your app to use, then in the devise_scope block, you must tell it to use admins/sessions, not devise/sessions, like this:
devise_scope :admin do
get 'login' => 'admins/sessions#new', :as => :new_admin_session
post 'login' => 'admins/sessions#create', :as => :admin_session
delete 'logout' => 'admins/sessions#destroy', :as => :destroy_admin_session
end
What about something like:
devise_for :admin, exclude: [:sessions] do
get '/login', to: 'sessions#new', as: :new_admin_session
post '/login', to: 'sessions#create', as: :admin_session
delete '/logout', to: 'sessions#destroy', as: :destroy_admin_session
end

Rails: How do I override Devise controller and Devise routes at the same time?

I am using Rails 4.0.2 and Devise 3.2.2 to handle user registration / authentication.
I have googled and search stackoverflow for answers, can't really find something that can answer my question.
The below code is my routes.rb, I have skip all sessions routes and registration routes but for some reason, Devise is not using my custom registrations_controller.rb because if it is, it should redirect to /pages/success (please see below my registrations_controller.rb )
routes.rb
App::Application.routes.draw do
resources :posts
resources :questions
get "users/:id", to: "users#show"
devise_for :users, :controllers => {:registrations => "registrations"}, :skip => [:sessions, :registrations]
as :user do
get 'login' => 'devise/sessions#new', :as => :new_user_session
post 'login' => 'devise/sessions#create', :as => :user_session
delete 'signout' => 'devise/sessions#destroy', :as => :destroy_user_session
end
as :user do
get '/' => 'devise/registrations#new', :as => :new_user_registration
post 'register' => 'devise/registrations#create', :as => :user_registration
end
get "registrations/update"
get "pages/home"
get "pages/privacy"
get "pages/terms"
get "pages/success"
end
registrations_controller.rb
class RegistrationsController < Devise::RegistrationsController
protected
def after_inactive_sign_up_path_for(resource)
'/pages/success'
end
end
There are several potential issues you may have:
Skip
If you're skipping the registrations functionality, I'd imagine it would prevent Devise from calling your RegistrationsController?
I would personally do this (correct your routes):
#config/routes.rb
root to: "users#index" (where ever your "logged-in" page is)
devise_for :users, path: "", controllers: { sessions: "sessions", registrations: "registrations" }, path_names: { sign_in: 'login', password: 'forgot', confirmation: 'confirm', unlock: 'unblock', sign_up: 'register', sign_out: 'signout'}
This will give you the routes you need, and will route to the "authenticated" index page in your app, thus either showing the login or registration page for Devise
Definition
The other issue you may have is an incorrect definition of your Devise Registrations controller. We use this code in a very recent development app:
#app/controllers/registrations_controller.rb
class RegistrationsController < ::Devise::RegistrationsController
end
Perhaps you could try using the :: before your Devise::RegistrationsController to see if it calls?

Devise custom routing links not working

I am running Rails 3.2.12 and Devise 3.1 and I have in the routes.rb this:
devise_for :users do
get '/login' => 'devise/sessions#new', as: :login
get '/logout' => 'devise/sessions#destroy', as: :logout
end
However, when I hit
127.0.0.1:3000/login
I get
No route matches [GET] "/login"
What works is
127.0.0.1:3000/users/login
Is there anything else I have to do so that I can skip typing /users/ part?
Thank you!
devise_for :users, :path => '', :path_names => { :sign_in => 'login'}
Good info about customizing Devise paths on this StackOverflow post :)
Here's some live code which works for one of our live apps:
#User Management (Devise)
devise_for :users, :path => '', :controllers => {:sessions => 'sessions', :registrations => 'registrations'}, :path_names => { :sign_in => 'login', :password => 'forgot', :confirmation => 'confirm', :unlock => 'unblock', :registration => 'register', :sign_up => 'new', :sign_out => 'logout'}
as :user do
get 'register', :to => 'devise/registrations#new'
delete 'logout', :to => 'sessions#destroy'
end
devise_scope :user do
get "/login" => "devise/sessions#new"
end
See this for more details

Rails Can't find route in Functional Test

Rails version 3.0.4 and Ruby 1.9.2
I'm using the devise gem, and have my application set up so that a user must sign in to perform any action. While writing my functional tests, I found that all of the test threw the same error.
`method_missing': undefined method `new_user_session_path'
This is strange as, if I rake routes I can see
new_user_session GET /devise/login(.:format) {:action=>"new", :controller=>"devise/sessions"}
I can also confirm that when I run the application, all of the link_tos work, when using that path.
Is there something special I have to do for a certain controller's test to see other routes?
routes.rb
Nge::Application.routes.draw do
resources :companies
resources :users
devise_for :users, :path => "devise", :path_names => { :sign_in => 'login', :sign_out => 'logout', :password => 'secret', :confirmation => 'verification', :unlock => 'unblock', :registration => 'register', :sign_up => 'cmon_let_me_in' }
...
end
test/functional/companies_controller_test.rb
require 'test_helper'
class CompaniesControllerTest < ActionController::TestCase
context "When a user is NOT signed in" do
[:index, :new].each do |action|
context "and GET ##{action.to_s}" do
setup {get action}
should set_the_flash.to(/must sign-up/)
should redirect_to(new_user_session_path)
end
end
...
end
end
You need to add
#request.env["devise.mapping"] = Devise.mappings[:user]
somewhere in your test, probably in the setup method.
See discussion here.
Can you try re-ordering the routes so that the devise routes are first?
So the routes.rb file would look something like this -
Nge::Application.routes.draw do
resources :companies
devise_for :users, :path => "devise", :path_names => { :sign_in => 'login', :sign_out => 'logout', :password => 'secret', :confirmation => 'verification', :unlock => 'unblock', :registration => 'register', :sign_up => 'cmon_let_me_in' }
resources :users
...
end

Resources