Remove model from Devise urls - ruby-on-rails

How do I remove the model name from Devise urls?
Ie
users/sign_in -> /sign_in
users/sign_out -> /sign_out
users/registration/sign_up -> /sign_up
users/registration/edit -> /edit

This is covered in the devise README (https://github.com/plataformatec/devise):
Devise also ships with default routes.
If you need to customize them, you
should probably be able to do it
through the devise_for method. It
accepts several options like
:class_name, :path_prefix and so on,
including the possibility to change
path names for I18n:
devise_for :users, :path => "usuarios", :path_names => { :sign_in => 'login', :sign_out => 'logout', :password => 'secret', :confirmation => 'verification', :unlock => 'unblock', :registration => 'register', :sign_up => 'cmon_let_me_in' }
Be sure to check
devise_for documentation for details.
If you have the need for more deep
customization, for instance to also
allow "/sign_in" besides
"/users/sign_in", all you need to do
is to create your routes normally and
wrap them in a devise_scope block in
the router:
devise_scope :user do
get "sign_in", :to => "devise/sessions#new"
end
This way
you tell devise to use the scope :user
when "/sign_in" is accessed. Notice
devise_scope is also aliased as as and
you can also give a block to
devise_for, resulting in the same
behavior:
devise_for :users do
get "sign_in", :to => "devise/sessions#new"
end
Feel free
to choose the one you prefer!
(Maybe this wasn't in the README when this question was originally posed.)

The way to do this manually is Rails 3 would be:
match 'sign_in' => 'devise/sessions#new', :as => :sign_in
match 'sign_out' => 'devise/sessions#destroy', :as => :sign_out

Related

Devise would not sign off users until I added this code. Why?

For some reason this time when I apply
<%= link_to "Sign Out", destroy_user_session_path, method: :delete%>
it returns a user/sign_out not found error
Digging through stackoverflow I found that by applying
devise_scope :user do
get '/users/sign_out' => 'devise/sessions#destroy'
end
to my routes.rb would allow it to work. And it did.
Afterwards, I decided to test it again, so I removed the above code, log in as the user, and magically the sign_out path link works again without the code.
What is going on here?
replice get in your route to delete:
devise_scope :user do
delete '/users/sign_out' => 'devise/sessions#destroy'
end
and i think that it should add other sessions action to your custom route and skip sessions controller in your devise route:
as :user do
delete '/users/sign_out' => 'devise/sessions#destroy', :as => :destroy_user_session
get '/users/signin' => 'devise/sessions#new', :as => :new_user_session
post '/users/signin' => 'devise/sessions#create', :as => :user_session
end
devise_for :users, :skip => [:sessions]

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

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

Devise - Changing the name of a routing

By default Devise creates a routing for a sign-in as '/sign_in'.
How would I change the path so that it is '/log_in'?
By adding scope
devise_for :users
devise_scope :user do
get '/login' => 'devise/sessions#new'
get '/logout' => 'devise/sessions#destroy'
end
This is how I did it in the end, given that I was already inserting controllers to handle aspects of Devise:
in routes.rb
#Add Devise authentication to users, handling omniauth callbacks in users/omniauth_callbacks_controller
devise_for :users, :skip => [:sessions],
:controllers => { :omniauth_callbacks => 'users/omniauth_callbacks',
:registrations => 'users/registrations'
}
# :skip => [:sessions] tells devise not to create routes for sessions, allowing us to declare our own
as :user do
get 'users/log_in' => 'devise/sessions#new', :as => :new_user_session
post 'users/log_in' => 'devise/sessions#create', :as => :user_session
delete 'users/log_out' => 'devise/sessions#destroy', :as => :destroy_user_session
end

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

Resources