Devise custom routing links not working - ruby-on-rails

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

Related

Ruby on Rails & Devise, change 'users/sign_in' to 'sign-in' using omniauth_callbacks and registrations

I am trying to change the 'users/sign_in' to 'sign-in' route
This is my current route setup
devise_for :users, :controllers => { :registrations => 'registrations', :omniauth_callbacks => "authentications" }
devise_scope :user do
put 'update_plan', :to => 'registrations#update_plan'
put 'update_card', :to => 'registrations#update_card'
put 'charge', :to => 'registrations#charge'
get "/sign-up" => "users/registrations#new", :as => :new_user_registration
get '/sign-in' => "devise/sessions#new", :as => :new_user_session
post '/sign-in' => 'devise/sessions#create', :as => :user_session
get '/sign-out' => 'devise/sessions#destroy', :as => :destroy_user_session
end
but these routes give me the error ArgumentError: Invalid route name, already in use: 'new_user_registration'
can I move the :controllers => { :registrations => 'registrations', :omniauth_callbacks => "authentications" } bit into devise_scope?
If i remove :as => :new_user_registration from my routes then i get a redirect loop. I really can't figure this out
Any help is much appreciated thanks.
You need to tell Devise to skip session ULRs.
devise_for :users :controllers => { :registrations => 'registrations', :omniauth_callbacks => "authentications" }, :skip => [:sessions]
devise_scope :user do
put 'update_plan', :to => 'registrations#update_plan'
put 'update_card', :to => 'registrations#update_card'
put 'charge', :to => 'registrations#charge'
get "/sign-up" => "users/registrations#new", :as => :new_user_registration
get '/sign-in' => "devise/sessions#new", :as => :new_user_session
post '/sign-in' => 'devise/sessions#create', :as => :user_session
get '/sign-out' => 'devise/sessions#destroy', :as => :destroy_user_session
end
See How To: Change the default sign_in and sign_out routes

Is it possible to have different paths on the same devise model with constraints?

I'm trying to have different paths for the same devise model/resource with constraints, but the first path is the one being applied in this case "visitor".
constraints(ValidSubdomainFrontend) do
devise_for :users, :path => "visitor", :path_names => { :sign_in => 'login', :sign_out => 'logout', :password => 'secret', :confirmation => 'verification', :unlock => 'unblock', :registration => 'register', :sign_up => 'cmon_let_me_in' }
end
constraints(ValidSubdomainAdmin) do
devise_for :users do
get 'users', :path => "admin", :to => 'site_backend#index', :as => :user_root # Rails 3
end
devise_for :users, :controllers => { :registrations => 'users' }, :path => "admin", :path_names => { :sign_in => 'login', :sign_out => 'logout', :password => 'secret', :confirmation => 'verification', :unlock => 'unblock', :registration => 'register', :sign_up => 'cmon_let_me_in' }
end
Is it possible to have different paths for the same resource with Devise on different constraints?
The constraints being used are:
class ValidSubdomainAdmin
def self.matches?(request)
request.subdomain.present? &&
request.env['PATH_INFO'].start_with?('/admin')
end
end
class ValidSubdomainFrontend
def self.matches?(request)
request.subdomain.present? &&
!request.env['PATH_INFO'].start_with?('/admin')
end
end
Is this possible with Devise at all or is this a Bug?
Found the solution for this issue!
More on the context, basically needed to have something like /visitor on the front-end where users can login (to the members area) and edit their profile and other members/clients operations. But I would also want /admin where admins/editors/authors can login to the (backend) admin dashboard/area. Both areas use the same Devise model/resource (so an admin can also visit the members area). As a side note, currently I'm using CanCan permissions to prevent members from accessing the admin area.
I just had to replace the admin area constraint for:
constraints(ValidSubdomainAdmin) do
devise_scope :user do
#root :to => "devise/registrations#new"
get "admin/" => "admin#index"
post 'admin/' => 'devise/registrations#new', :as => :new_admin_registration
match 'admin/', :to => 'admin#index'
get "admin/edit" => "devise/registrations#edit"
match 'admin/edit', :to => 'devise/registrations#edit'
get "admin/login" => "devise/sessions#new", :as => :new_admin_session
match 'admin/login', :to => 'devise/sessions#new'
get "admin/logout" => "devise/sessions#destroy", :as => 'destroy_admin_session'
match 'admin/logout', :to => 'devise/sessions#destroy'
post "admin/password" => "devise/passwords#create"
get "admin/password/new" => "devise/passwords#new", :as => 'new_admin_password'
get "admin/password/edit" => "devise/passwords#edit"
put "admin/password" => "devise/passwords#update"
end
end
I also updated the views /admin/users (being used by devise on the on the admin area) to use the new admin devise paths.
Many thanks to José Valim

Make devise paths unavailable

How can i make devise paths unavailable. Now i have all forms for registration, authorization and password restore on one page.
Here are my routes
devise_for :users, :controllers => { :passwords => "passwords", :registrations => "registrations" }, :path => 'accounts', :path_names => { :sign_in => 'login', :sign_up => 'new', :sign_out => 'logout'}
so if i enter something like accounts/new in url, i will get to devise sign up page. Is it possible to make all devise pages unavailable? I should get RoutingError or MissingTemplate for example if i enter either of the devise routes.
Just skip the parts you don't want:
devise_for :users,
:controllers => {
:passwords => "passwords",
:registrations => "registrations" },
:path => 'accounts',
:path_names => {
:sign_in => 'login',
:sign_up => 'new',
:sign_out => 'logout'},
:skip => [:passwords, :registrations, :sessions]

Devise custom routes slippery slope

I started using custom routes w/Devise so that I could have my 'Sign In' and 'Sign Up' routes go to the same page. However, as soon as I followed the instruction from Devise about custom routes, it seems that every route now has to be explicitly specified. This has now broken my reset password links since that portion is handled by Devise.
What am I doing wrong here? You can see below that I've had to spell out everything for my User and UserSessions model. Shouldn't I only have to specify the ones I want to change?
devise_for :users, :controllers => { :sessions => "user_sessions" ,:registrations=>"users"},:skip => [:sessions] do
get 'users/sign_in' => 'user_sessions#new', :as => :new_user_session
get 'users/sign_up' => 'user_sessions#new', :as => :new_user_session
post 'users/sign_in' => 'user_sessions#create', :as => :user_session
post 'user_sessions' => 'user_sessions#create', :as => :app_sign_in
delete 'users/sign_out' => 'user_sessions#destroy', :as => :destroy_user_session
get 'users/sign_out' => 'user_sessions#destroy', :as => :destroy_user_session
post 'users/:id' => 'users#update', :as =>:update_user
get 'users' => 'users#index'
get 'users/:id/edit' => 'users#edit', :as => :edit_user
get 'users/:id' => 'users#show', :as => :show_user
delete 'users/:id' => 'users#destroy', :as => :destroy_user
end
Can you just use, not sure if this will work for you
devise_for :users
devise_for :users do
get '/users/sign_out' => 'devise/sessions#destroy'
end

Changing the sign_in url for Devise

How do you change the sign in path for Devise when using a before_filter :athenticate user?
I have the following in a Posts controller.
eg:
class PostsController < ApplicationController
before_filter :authenticate_user!
def index
#posts = Post.all
end
end
At the moment it automatically goes to '/users/sign_in'
I'd like to use '/login'
Sorted for now folks, using the devise_for method.
devise_for :users, :controllers => { :registrations => 'registrations' }, :path => 'accounts', :path_names => { :sign_in => 'login', :sign_up => 'new', :sign_out => 'logout', :password => 'secret', :confirmation => 'verification' }
So now the sign_in path is 'accounts/login'
That solution doesn't modify the resource path for sign_in.
I did however sort it out via the devise_for method. eg:
devise_for :users,
:controllers => { :registrations => 'registrations' },
:path => 'accounts',
:path_names => { :sign_in => 'login',
:sign_up => 'new',
:sign_out => 'logout',
:password => 'secret',
:confirmation => 'verification' }
So now the sign_in path is 'accounts/login'
I think the info you are looking for is here: https://github.com/plataformatec/devise/wiki/How-To:-Change-the-default-sign_in-and-sign_out-routes
Stolen from the docs:
devise_scope :user do
get "/login" => "devise/sessions#new"
end
In you case you would use :post instead of :user I believe.
Its late and I am fuzzy headed but I think that should do it.

Resources