Rails 4, Devise Routes - ruby-on-rails

I've created custom routes to route to the devise login and logout paths:
devise_scope :admin do
get "logout" => "devise/sessions#destroy", as: :logout
get "login" => "devise/sessions#new", as: :login
end
This works. The only problem is that if the the login fails it redirects back to admins/sign_in instead of /login.
Any ideas?

According to this answer and this description, it seems the proper way to achieve what you're attempting to do is to make use of the :path_names option. According to the description from the Devise wiki:
devise_for :admin, :path => '', :path_names => {:sign_in => 'login', :sign_out => 'logout'}
will create the normal admin routes for you, and will assign the /sign_in and /sign_out route to /login/ and /logout respectively.
Using the :path option, you can further alter the URL, such as using :path=>"admins" will yield routes like /admin/login, etc.

Related

Change the default Devise GET user path

I want to change the default path of some user (in this case with id = 1) information from:
domain.com/user.1
to
domain.com/user/1
I already use devise_for on my route.rb, is there some special command to do what I need with this?
this is my route.rb
devise_for :users,
:path => '',
:path_names => {
:sign_in => 'login',
:sign_out => 'logout',
:sign_up => 'register'
},
:controllers => { registrations: 'registrations' }
First, domain.com/user.1 doesn't look like a real route to me. Are you sure you're not calling users_path(id) when you intend to call user_path(id)?
Also, a user/:id route doesn't look like it has anything to do with Devise, which is concerned with authentication/authorization. It looks more like a show resource method that would go in UsersController#show.
In any case, the following route should give you the /user/:id route which maps to UsersController#show
resources :user
which would create the following route helper method:
user_path(id)

Rails routing to multiple route

I'm using devise for rails.
I have the following route for devise.
devise_for :user
Which routes to 'user/sign_in' and several other.
So I want to change this route to: get 'login'. Is this possible?
I tried doing
match 'login', to: 'user/sign_in', via: :get
Which did not work as well, what am I doing wrong, and what does the above code do?
To use /login for sign_in add the following to your config/routes.rb:
devise_scope :user do
get 'login', to: 'devise/sessions#new'
end
This'll work:
devise_for :user, :path => 'login'
You might need :users and not :user, FYI.

Issue with rails controller

I have a controller pages as follow:
class PagesController < ApplicationController
def home
end
def about
end
def login
end
end
i have the views corresponding also , and in my routes.rb, i have the following
devise_for :users
get 'log in' => 'pages#login'
get 'about' => 'pages#about'
root :to => 'pages#home'
when i try to go to the login page it's gave me an error :
undefined local variable or method `login_path' for #<#:0x2b9a298>
i try to match the controller to the actions ,same error . i'm new with rails , i'm trying to understand what i did wrong , because it works for the 'about' page. thanks
Your routes should be:
match 'login', :to => "pages#login", :as => :login
match 'about', :to => "pages#about", :as => :about
To learn more about Rails routing, check out the routing guide.
get 'log in' => 'pages#login'
Is there a space in "log in" or is that just how it appears here?
I think Devise also has some special setup in how you customize the routes. Here is an example provided on their GitHub page:
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' }
It's at https://github.com/plataformatec/devise under Configuring Routes.

Restrict access or redirect devise path users/sign_in

I am creating a rails app. And i have login in such a way that, users are directed or redirect to /login for signing in. And when users/sign_in is passed the it embeds the sign in form into the application layout which i don't want users to see. Is there a way i can redirect users/sign_in to /login or restrict access to it ?
thanks
The following will replace the routes:
devise_for :model_name, :path => '',
:path_names => {:sign_in => 'login', :sign_out => 'logout'}
It will replace the users/sign_in and users/sign_out routes with login/logout, and the redirects will take them to those routes accordingly as well.
try this. It should works. ;)
config/routes.rb
# change :devise_model to :user, or :admin or any name of your devise model
devise_scope :devise_model do
get '/login' => "devise/sessions#new"
end
and you can use this in view like this.
link_to 'Login', login_url
PS. if you have override devise controller. You should tell the router first, and change devise controller to your override controller name.
go to this link and see Configuring controllers content.

Devise showing error 'invalid email or password' upon going to login

I'm using Rails 3 with Devise and have setup my routes.rb file like so:
devise_for :users,
:path_names => { :sign_in => 'login', :sign_out => 'logout'}
devise_scope :user do
get '/login' => 'devise/sessions#create'
get '/logout' => 'devise/sessions#destroy'
end
resources :users
When I go to /login I get the flash messages:
Signed out successfully.
Invalid email or password.
The first message is a notice and I'm not worried about it, but the second one is an alert and is annoying as the user hasn't hit sign in yet and it's already complaining that there is no password.
Is there an easy way to suppress this message? Have I setup devise wrong maybe.
I'm using username field to login instead of email.
I have changed my devise.rb to have
config.authentication_keys = [ :username ]
This was an easy mistake when I look back at it with a fresh head:
This
get '/login' => 'devise/sessions#create'
Should be
get '/login' => 'devise/sessions#new'

Resources