How to develop a destruction session and exit the controller?

Think of destroying a session and exit the controller?

if something_is_not_kosher # 1. log this event, 2. send notice redirect_to destroy_user_session_path and return end 

Also tried:

 if something_is_not_kosher # 1. log this event, 2. send notice redirect_to controller: 'devise/sessions', action: 'destroy', method: :delete and return end 

Error No route matches [GET] "/users/sign_out" , but I explicitly set the :: delete method in Example 2. Maybe the method has a method? current_user.sign_out and tried sign_out (current_user), which also doesn't work? Thanks for the help.

rake routes:

  new_user_session GET /users/sign_in(.:format) devise/sessions#new user_session POST /users/sign_in(.:format) devise/sessions#create destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy user_password POST /users/password(.:format) devise/passwords#create new_user_password GET /users/password/new(.:format) devise/passwords#new edit_user_password GET /users/password/edit(.:format) devise/passwords#edit PATCH /users/password(.:format) devise/passwords#update PUT /users/password(.:format) devise/passwords#update cancel_user_registration GET /users/cancel(.:format) users/registrations#cancel user_registration POST /users(.:format) users/registrations#create new_user_registration GET /users/sign_up(.:format) users/registrations#new edit_user_registration GET /users/edit(.:format) users/registrations#edit PATCH /users(.:format) users/registrations#update PUT /users(.:format) users/registrations#update DELETE /users(.:format) users/registrations#destroy 
+6
source share
4 answers

So, I decided to solve this by creating a custom distribution route

  devise_scope :user do get '/signout', to: 'devise/sessions#destroy', as: :signout end 

and in my controller I:

 if something_is_not_kosher redirect_to signout_path and return end 
+5
source

Why don't you just use the built-in method sign_out_and_redirect(current_user) ?

+9
source

destroy_user_session_path(@user) is the exit path for the user, but he must request the DELETE method. redirect_to method will tell broswer to request a different path, but broswer can simply request a GET method.
Therefore, if you want the user to log out, you must set the checkout form using the DELETE method or using an AJAX request so that the user can log out, but not with the redirect_to function.

If you just want to destroy the user session, use sign_out @user in order.

+5
source

Just in case, someone cannot use it directly.

  <% if user_signed_in? %> <li><%= link_to "Logout", destroy_user_session_path, :method => :delete %></li> <% else %> <li><%= link_to "Sign up now!", new_user_registration_path%></li> <% end %> 
+2
source

Source: https://habr.com/ru/post/975331/


All Articles