Devise logout link doesn't work - ruby-on-rails

Why click on link = link_to 'Logout', destroy_user_session_path, method: :delete make GET request to /users/sign_out and render show view of UsersController? My routes.rb is:
devise_for :users
resources :users, only: [:show] do
member do
get 'profile'
get 'purchases'
get 'mailing'
end
end
and /config/initializers/devise.rb contain config.sign_out_via = :delete, rake routes output is:
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) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
profile_user GET /users/:id/profile(.:format) users#profile
purchases_user GET /users/:id/purchases(.:format) users#purchases
mailing_user GET /users/:id/mailing(.:format) users#mailing
user GET /users/:id(.:format) users#show

The html generated for your logout link is as follows:
<a rel="nofollow" href="/users/sign_out" data-method="delete"></a>
This will only work if the js files are properly loaded ( I think it needs jquery_ujs )
Otherwise it defaults to a GET request.

It should be work for you.
<%= link_to "Sign out", destroy_user_session_path, :method => :delete %>

Related

Devise conflicting with other routes using delete

I have these 2 routes that are conflicting in my application
destroy_users DELETE /users/:id(.:format) users#destroy
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
the corresponding part of my routes file is as follows
match '/users/:id', to: 'users#destroy', :via => :delete, :as =>:destroy_users
devise_for :users
resources :users
When I try to destroy a session using the following link
<li><%= link_to "Sign out", destroy_user_session_path, method: "delete" %></li>
it tries to navigate to the following route
localhost:3000/users/sign_out
this is going to my UserController and trying to run the destroy method passing sign_out as a parameter rather than a route
Couldn't find User with id=sign_out
The first line in my routes file was to allow a single user to be deleted in the UserController, this is the destroy method. This had to be put before the devise_for otherwise it was trying to route to the edit_user_registration_path of devise using DELETE. Now it seems to be overwriting the devise destroy_user_session_path
I'm not sure how to fix this any advice would be appreciated
Typically, I would think devise_for :users creates the following routes
Prefix Verb URI Pattern Controller#Action
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) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
using your routes you get
Prefix Verb URI Pattern Controller#Action
destroy_users DELETE /users/:id(.:format) users#destroy <--- oops this should be below all the other 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) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy <--- notice it typically would be
Deleting the first match route should help you... or you can move it below your resources :users. You should skip creating destroy with resources.

Using a bootstrap button with link to devise page?

Full disclosure, I'm a no00b. That said... I'm trying to use a bootstrap button that when clicked links to a devise registration page. I either get an error that I have an undefined method or it completely messes up my bootstrap format. Can someone please explain what is going on?
This line of code messes up the bootstrap formatting
<%= link_to "Sign Up", new_user_registration_path, :class => "btn btn-success" %>
Edit: rake routes path shows I have new_user_registration_path
new_user_session GET /auth/login(.:format) devise/sessions#new
user_session POST /auth/login(.:format) devise/sessions#create
destroy_user_session DELETE /auth/logout(.:format) devise/sessions#destroy
user_password POST /auth/secret(.:format) devise/passwords#create
new_user_password GET /auth/secret/new(.:format) devise/passwords#new
edit_user_password GET /auth/secret/edit(.:format) devise/passwords#edit
PATCH /auth/secret(.:format) devise/passwords#update
PUT /auth/secret(.:format) devise/passwords#update
cancel_user_registration GET /auth/register/cancel(.:format) devise/registrations#cancel
user_registration POST /auth/register(.:format) devise/registrations#create
new_user_registration GET /auth/register/cmon_let_me_in(.:format) devise/registrations#new
edit_user_registration GET /auth/register/edit(.:format) devise/registrations#edit
PATCH /auth/register(.:format) devise/registrations#update
PUT /auth/register(.:format) devise/registrations#update
DELETE /auth/register(.:format) devise/registrations#destroy

Signing out with Devise not working

Im having problems with signing out with devise, i get the following error message when i try to sign out, its looking for a user with the id=sign_out. Any suggestions?
ActiveRecord::RecordNotFound in UsersController#show
Couldn't find User with id=sign_out
Routes
devise_for :users
match 'users/settings', to: 'users#settings'
resources :users, only: [:show, :update]
Link
<%= link_to 'Logout', destroy_user_session_path, :method => :delete %>
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
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
users_settings /users/settings(.:format) users#settings
user GET /users/:id(.:format) users#show
PUT /users/:id(.:format)
Recently I've solved this problem, You need try to put or check the below line in your views/layouts/application.html.erb
<%= javascript_include_tag "application" %>
Also check if you have jquery_ujs in your Gemfile.
Make sure you're actually clicking on the correct link. I highly doubt that link routes the request to UsersController#show. destroy_user_session_path routes to /users/sign_out so this is impossible.

Rails Devise uninitialized constant Model

My Rails version is 3.2.12. I use following commands to create users authentications:
rails generate devise:install
rails generate devise users
rake db:migrate
and following are my 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
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
But i still cant register new users or login into app even though user email and password are saved on db. Following are errors that i got:
NameError in Devise::RegistrationsController#create
uninitialized constant Model
Where i might be wrong?
try this to change this line for devise.rb
config.sign_out_via = :delete
to
config.sign_out_via = :get
It works for me
Try this in your routes,
devise_for :users, :controllers => {:registrations => "devise/registrations"} do
get '/register' => 'devise/registrations#new', :as => :new_user_registration
end

be redirected to users/sign_in

I have a controller of Workers.
If I logged out, I am redirected to: localhost:3000. I want to be redirect to the sign_in of my devise.
this is my rake routes:
users_sign_out GET /users/sign_out(.:format) devise/sessions#destroy
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
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
and this is my routes.rb:
devise_for :users do get '/users/sign_out' => 'devise/sessions#destroy' end
resources :tasksadmins
resources :workers
root to: "devise/registrations#create"
It is specified in the devise wiki.
https://github.com/plataformatec/devise/wiki/How-To:-Change-the-redirect-path-after-destroying-a-session-i.e.-signing-out
class ApplicationController < ActionController::Base
private
# Overwriting the sign_out redirect path method
def after_sign_out_path_for(resource_or_scope)
new_user_session_path
end
end
In you can override the after_sign_out_path_for helper in your application controller.
def after_sign_out_path_for(resource_or_scope)
# logic here
end

Resources