Devise would not sign off users until I added this code. Why? - ruby-on-rails

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]

Related

Setting devise routes with locales failing on redirect after authentication

So I've been trying to set my routes for devise as follows:
scope '(:locale)' do
devise_for :users, skip: [:registrations]
resources :questions
end
However when I authenticate in my controller, when you're not signed in devise redirects you to localhost:3000/users/sign_in.
What I expect to happen would it to redirect me to localhost:3000/en/users/sign_in. What am I doing wrong or what way is there around this?
So it turns out you can't set the routes in your scope, but rather need to set them manually. At least that's what's worked for me.
devise_for :users, skip: [:sessions]
as :user do
get ':locale/login' => 'devise/sessions#new', :as => :new_user_session
post ':locale/login' => 'devise/sessions#create', :as => :user_session
delete ':locale/logout' => 'devise/sessions#destroy', :as => :destroy_user_session
end
# your normal scope without devise_for in it
scope '(:locale)' do
resources :questions
end
After this the redirects work as expected.

Devise: Issue with routing using devise_scope

I'm trying to route my User model sign up form to /sign_in. Tried several things and just used the example given in the devise docs.
I copied in to my routes.rb
devise_scope :users do
get "sign_in", to: "devise/sessions#new"
end
and rake routes says it should be working and shows what I expected.
http://s1.postimg.org/do335u8v3/Screen_Shot_2014_06_18_at_11_34_34.png
What's going wrong here?
Try this instead.
devise_for :users do
get '/users/sign_in', :to => 'devise/sessions#new', :as => :new_user_session
get '/users/sign_out', :to => 'devise/sessions#destroy', :as => :destroy_user_session
get "/users/sign_up", :to => "registrations#new"
end

Devise redirecting after errors

On the sign up and forgot password views in Devise, if you get an error, it redirects you to the parent page.
So on the sign up page (/users/sign_up), if you get an error, it redirects you to /users and shows the error.
On the forgot password page (/users/password/new), if you get an error, it redirects you to /users/password and shows the error.
How can I change it so it does the same thing as the sign in page, if there's an error, it stays on the same page and shows the error.
I've looked through Devise and can't find where the redirect is.
Here's my routes for Devise:
devise_for :users, :skip => [:sessions]
as :user do
get 'signin' => 'devise/sessions#new', :as => :new_user_session
post 'signin' => 'devise/sessions#create', :as => :user_session
get 'signup' => 'devise/registrations#new', :as => :new_user
post 'signup' => 'devise/registrations#create', :as => :create_user_session
delete 'signout' => 'devise/sessions#destroy', :as => :destroy_user_session
get "/account" => "devise/registrations#edit"
end
I think the problem is that you have the post 'signup' named incorrectly. What path does your user signup form POST to?
post 'signup' => 'devise/registrations#create', :as => :create_user_session
Should be:
post 'signup' => 'devise/registrations#create', :as => :user_registration
Here's a look at my routes.rb which solved this issue:
as :user do
get "/signin" => "devise/sessions#new", :as => :new_user_session
post "/signin" => "devise/sessions#create", :as => :user_session
delete "/signout" => "devise/sessions#destroy", :as => :destroy_user_session
get "/signup" => "devise/registrations#new", :as => :new_user_registration
post '/signup' => 'devise/registrations#create', :as => :user_registration
end
It doesn't redirect you anywhere, those are the URLs that Devise posts to.
If you want to edit these URLs, see the wiki for a good starting point:
https://github.com/plataformatec/devise/wiki/How-To:-Change-the-default-sign_in-and-sign_out-routes

devise won't redirect to after_sign_up_path when using customized devise routes

I invoke the after_sign_up_path_for(resource) by defining it in an inherited registrations_controller:
class RegistrationsController < Devise::RegistrationsController
protected
def after_sign_up_path_for(resource)
new_user_profile_path(resource)
end
end
This redirect works fine if I don't customize the routes. however, when I do, it no longer works. My routes file has:
devise_for :users, skip: [:sessions], controllers: { registrations: "registrations" }
devise_scope :user do
get 'signin' => 'devise/sessions#new', as: :new_user_session
post 'signin' => 'devise/sessions#create', as: :user_session
delete 'signout' => 'devise/sessions#destroy', as: :destroy_user_session
get 'signup' => 'devise/registrations#new', as: :new_user_registration
post 'signup' => 'devise/registrations#create', as: :user_registration
root to: 'pages#home'
end
Specifically, the post 'signup' => 'devise/registrations#create', as: :user_registration line messes things up. If that's not there, it works fine. However, if that's not there, then when registration fails due to validation error, the URL defaults to /users rather than /signup, which is an undesired behavior.
Anyone know if this is something I am doing wrong or if there's a bug in devise?
The issue with your custom routes is that you are pointed back to Devise controllers:
post 'signup' => 'devise/registrations#create', as: :user_registration
That obviously is not going to work. Point to your own controller instead:
post 'signup' => 'registrations#create', as: :user_registration
Just briefly looking at your code provided, I don't think the route you want is new_user_profile_path. You probably want user_profile_path, can you run rake routes and paste the output here?

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