简体   繁体   中英

Rails 3.2 with Devise: logout link stopped working

The log out link in my app has been working fine, then it just stopped working. I haven't tested it for a while, but I haven't made any changes to anything related to it for a month! It is sending a GET request when it should be interpreted by the controller as a DELETE request. Here is my code:

My ERB file in my view includes this line:

<li><%= link_to('Log Out', destroy_user_session_path, :method => :delete)%></li>

and also includes this:

<%= javascript_include_tag "application" %>

This is from my application.js :

//
//= require jquery
//= require jquery_ujs
//= require underscore
//= require backbone
//= require jquery.serializeJSON
//= require jquery-ui-1.10.3.custom
//= require jquery-ui-touchpunch.min.js
//= require auto_form_bot
//= require_tree ../templates
//= require_tree ./models
//= require_tree ./collections
//= require_tree ./views
//= require_tree ./routers
//= require_tree .

And here is the pertinent stuff from my gemfile:

source 'https://rubygems.org'
ruby "1.9.3"

gem 'rails', '3.2.13'
gem 'backbone-on-rails', '~> 1.1'
gem 'compass-rails'
gem 'devise', '>= 3.1.0'
gem 'ejs'
gem 'figaro'
gem 'jquery-rails'
gem 'pg'
gem 'sass-rails',   '~> 3.2.3'

This is the actual html that is rendered:

<li><a href="/users/sign_out" data-method="delete" rel="nofollow">Log Out</a></li>

When I click on the link, here is the error I get:

Routing Error
No route matches [GET] "/users/sign_out"

And here is my server log:

Started GET "/users/sign_out" for 127.0.0.1 at 2013-12-19 23:49:24 -0500

ActionController::RoutingError (No route matches [GET] "/users/sign_out")

Here are the relevant 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

The GET request generated by the link should automatically be changed to a DELETE request, but it isn't anymore, and I don't know what I changed to make it stop working.

Even stranger, every once in a while it actually works! And the same link works on another page in my app. Here is a link to the demo account of my app, where you will be logged in automatically, so you can log out: http://www.form-itable.com/accounts/demo

I can't figure out what I am missing here. I know I can make a workaround, but the link should work and worked fine until now, and I want to know why it isn't.

As I was reading through the jquery_ujs files I saw that it appears that it turns links from GET to DELETE by setting up an event listener on the document , like so:

// Link elements bound by jquery-ujs
linkClickSelector: 'a[data-confirm], a[data-method], a[data-remote], a[data-disable-with]'

...

$document.delegate(rails.linkClickSelector, 'click.rails', function(e) {
  var link = $(this), method = link.data('method'), data = link.data('params'), metaClick = e.metaKey || e.ctrlKey;
... more code ...

In my app at one point, I had a complicated situation where I needed to remove a click event from the document , and since I wasn't using any other click handlers, I just called $(document).off('click'); which removes everything that is listening to click events on the document . This, apparently, is a very bad thing. I replaced that line with something that specifically removed my click handler, and everything works again.

The lesson is that if you are using jQuery_ujs do not ever use off() on the document unless you can target a specific handler .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM