rails delete user session path - ruby-on-rails

I am using the devise gem to make authentication work in my app.
Here's the code I have for signing out:
<%= link_to "Sign out", destroy_user_session_path, :method => :delete %>
I have tried this as well:
<%= link_to "Sign out", destroy_user_session_path%>
Both of which when I click on sign out I get :
No route matches [GET] "/users/destroy"
However when I run rake routes, you can see it (just not GET):
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
How to fix this?

Mitch's answer was close but did not work for me, instead the following syntax did:
<%= link_to "Log out", destroy_user_session_path, :method => :delete %>

Could this help you in the right direction?
https://github.com/plataformatec/devise/issues/1195

Roger's link above is v useful.
I used the following syntax, which worked:
<%= link_to "Log out", destroy_session_path(:user), :method => :delete %>

Related

Using link_to on a list element in rails

I'm trying to making the final list element a clickable link to my search_path in rails using HAML but have been unable to make it work.
Here is what I have so far:
%li
= link_to('sign out', destroy_user_session_path, :method => :delete)
%li
= link_to "profile", edit_user_registration_path
%li{:style => 'background-color:#3D8599;'}
%i.fa.fa-search
Everything I have tried results in the fa-search icon being put in the wrong place or not being clickable.
Have you tried this?
%li{:style => 'background-color:#3D8599;'}
= link_to search_path do
%i.fa.fa-search

Unable to destroy session using Devise in Rails [duplicate]

This question already has answers here:
No route matches "/users/sign_out" devise rails 3
(31 answers)
Closed 8 years ago.
Noob here building my first Rails 4.0 app using Devise. Set up my controller and routes using the standard Devise tactics.
Rake Routes shows:
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
In my Nav, my code is:
<%= link_to "Sign Out", destroy_user_session_path, method: :destroy, data: { confirm: 'Are you sure?' } %>
I'm running the Guard gem so my server is always on, so I know it's not a mismatch b/w my routes and the server. I still get the following error:
No route matches [POST] "/users/sign_out"
Any suggestions? Thanks all!
You probably need to change the method (http verb) option to delete:
<%= link_to "Sign Out", destroy_user_session_path, method: :delete, data: { confirm: 'Are you sure?' } %>
Check in config/initializers/devise.rb and make sure this is set:
config.sign_out_via = :delete
Also, in the view I have this and works just fine:
<%= link_to 'Logout', destroy_user_session_path, :method=>'delete' %>

Destroy Session, Rails Devise

I've set up Devise to authenticate/register users.
But having problems signing them out.
Have this link:
<%= link_to "Sign out", destroy_user_session_path, :method => :delete %>
It looks like this in HTML:
Sign Out
When I click it - get this error:
No route matches [GET] "/users/sign_out"
First, make sure you have <%= javascript_include_tag :defaults %> in your layout file "application.html.erb."
Then, in your config -> initializers -> "devise.rb" file make sure it says:
config.sign_out_via = :delete
and your "sign_out" code destroy_user_session_path, :method => :delete should work.
If it still doesn't work please comment!
Good luck.

adding a method and html options to link_to in rails?

I'm brand new to ruby and rails and I'm having trouble creating a sign-out link (using devise for auth). I want to pass a custom :method parameter into the link_to function, and set a custom class. I seem to be able to do one or the other but not both.
When I try:
<%= link_to "Sign out", destroy_user_session_path, :method => :delete, { :class => 'signout'} %>
I get the proper result from clicking the link, but I lose my styling. On the other hand, when I try:
<%= link_to "Sign out", destroy_user_session_path, { :class => 'signout'}, :method => :delete %>
I get the styling I want but the link request is passed as GET rather than DELETE, resulting in a routing error.
What am I missing?
Try placing both :class and :method inside the hash.
So:
<%= link_to "Sign out", destroy_user_session_path, { :class => 'signout', :method => :delete} %>
You don't need hash here, because it is already hash :D
link_to "Sign out", destroy_user_session_path, method: "delete", class: "signout"
profit!

Devise/Rails: No route matches [GET] "/users/sign_out"

I'm a bit confused about how devise is routing my requests, for some reason I can't go to the sign-out path in my app now:
ActionController::RoutingError (No route matches [GET] "/users/sign_out")
Here is what my routes related to my User model and Devise look like:
devise_for :users, :controllers => {:registrations => "registrations"}
devise_scope :user do
get '/settings' => 'registrations#edit'
end
Would defining that scope prevent my other routes from working as well?
Update
I don't think that it's supposed to be GET request, as my link looks like:
<%= link_to "Sign out", destroy_user_session_path, :method => :delete %>
If you try to go to /users/sign_out by typing it in the address bar of your browser, or linking to it normally, you will get this error. To summarize some comments and answers here and from issues at the devise repo on github:
This is the intended functionality, RESTful apps should not modify state with a GET request.
You can fix this by making a link that uses the DELETE method as mentioned by #Trip, #fuzzyalej, and the update by #Joseph Silvashy.
Alternatively, (and less recommended), in /config/initializers/devise.rb, you could make the following change
config.sign_out_via = :delete
to
config.sign_out_via = :get
I came across this issue while following along with http://railscasts.com/episodes/209-introducing-devise and with the older version of devise he's using there, this was not an issue.
Keep your devise.rb using the correct HTTP method:
# good
config.sign_out_via = :delete
# bad
config.sign_out_via = :get
Use button_to instead of link_to
# good
= button_to "Sign Out", destroy_user_session_path, method: :delete
# bad
= link_to "Sign Out", destroy_user_session_path, method: :delete"
If you are using bootstrap (keep it classy)
= link_to "Sign Out", destroy_user_session_path, method: :delete, class: "btn btn-default btn-sm"
Just so this post has an answer :
This must be sent with the method, delete so that it does not default to a get
<%= link_to "Sign out", destroy_user_session_path, :method => :delete %>
devise_for :users
devise_scope :user do
get '/users/sign_out' => 'devise/sessions#destroy'
end
in your routes.rb.
If you're using SSL with Devise controllers, and trying to sign out from an 'http' url, what's happening is it's sending the DELETE request to the #destroy method, but then redirecting to the https version via GET. You can fix this by adding the https to the sign out link like so:
= link_to "Sign out", destroy_user_session_url(protocol: 'https'), method: :delete
Note: must be 'url' instead of 'path'
in Rails 7 the destroy_user_session_path changes a bit on the method.
instead of using the traditional method: :delete that came with the jquery and rails_ujs. Now you need to change that to ..., data: {turbo_method: :delete} and you don't need to setup any additional rails_ujs or jquery
From the rails documentation:
link_to("Destroy", "http://www.example.com", :method => :delete, :confirm => "Are you sure?")
# => Destroy
I had the same problem and to solve it took these steps:
1) include the //= require jquery_ujs in the assets/javascripts/application.js
2) add gem 'jquery-rails' to my gemfile
3) bundle install
... and it worked.

Resources