0
votes

I have have devise set up for my User model.

However, when I try to call the destroy session path:

link_to "Sign out", destroy_user_session_path

But when I click on the link it sends me to

.../users/sign_out

I get the following error, because sign_out is not valid user obviously:

ActiveRecord::RecordNotFound in UsersController#show

How do I get around this? I have tried this but it doesn't even seem to recognize it's n the routes.

devise_scope :users do
  delete "user/signout" => "devise/sessions#destroy", :as => :destroy_session
end
1

1 Answers

2
votes

In your routes file, you've specified that this route should be matched on a DELETE request. However, your signout link is sending a GET request. This is why the request is matching UserControllers#show.

Assuming you're using jquery, you can set up this link to send the signout request as DELETE by making sure you've added jquery_ujs to the included javascript. This will modify the request method for links that have the data-method attribute set to something other than 'GET', To specify the proper :method param for your signout link_to, use:

link_to "Sign out", destroy_user_session_path, method: :delete

Of course, this will only work if your users have Javascript enabled; otherwise, the request will fallback to GET anyways. If you want to make signout work for GET requests, it will need to have higher precedence in your routes file than the show route.