Rails 3.2与Devise:注销链接停止工作 [英] Rails 3.2 with Devise: logout link stopped working
问题描述
我的应用程序中的注销链接已正常工作,然后停止工作。我没有测试过一段时间,但是我没有对任何与它相关的一个月进行任何更改!当控制器将其解释为 DELETE
请求时,它将发送一个 GET
请求。这是我的代码:
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:
我的视图中的ERB文件包括以下行:
My ERB file in my view includes this line:
<li><%= link_to('Log Out', destroy_user_session_path, :method => :delete)%></li>
,还包括:
<%= javascript_include_tag "application" %>
这是从我的 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'
这是呈现的实际html:
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")
以下是相关路线:
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
GET
链接生成的请求应该自动更改为
DELETE
请求,但不再是,我不知道我改变了什么它停止工作。
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.
甚至陌生人,每一次,它实际上工作!而且我的应用程序中的另一个页面上也有相同的链接。这是一个指向我的应用程序的模拟帐户的链接,您将自动登录,以便您可以注销: http://www.form-itable.com/accounts/demo
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.
推荐答案
当我正在阅读 jquery_ujs文件时,看到它似乎变成了通过在文档上设置事件侦听器
到 DELETE
的链接 $ c>,像这样:
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 ...
在我的应用程序中,我有一个复杂的我需要的情况从文档
中删除点击
事件,由于我没有使用任何其他点击处理程序,我刚刚调用 $(document).off('click');
将删除在文档上收听点击事件的所有内容
。这显然是一件非常糟糕的事情。我替换了那条专门删除我的点击处理程序的东西,并且一切正常工作。
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.
这个教训是,如果你使用jQuery_ujs 不要使用 off()
在文档
,除非您可以定位特定的处理程序。
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.
这篇关于Rails 3.2与Devise:注销链接停止工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!