Ajax删除链接注销current_user [英] Ajax Delete links log out current_user

查看:176
本文介绍了Ajax删除链接注销current_user的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标题几乎解释了这一点。我有一个奇怪的情况,其中允许用户使用Ajax删除通知的视图导致current_user被注销。我甚至不知道从哪里开始调试这个...



这是控制器

  class NotificationsController< ApplicationController 

def destroy
@notification = Notification.find(params [:id])
@ notification.destroy
respond_to do | format |
format.js
end
end


end

这是整个控制器,没有什么是简短的。通知是由系统生成的,所以用户唯一可以采取的操作是关闭(即删除)。



我也尝试过使用较新的 respond_with 语法,效果相同。



我正在使用Devise和Rails 3.0.9。任何想法可能会发生什么 - 或关于如何调试的建议?



- 编辑1 -



Routes.rb

 资源:notifications,:only => [:destroy] 

删除链接

 %span.delete = link_to('dismiss',notification_path(notification),:method =>:delete,:remote => true)

- 编辑2 -



嗯,我在日志中注意到新的东西 - - 见下面的****。

 在2011-06-21启动DELETE/ notifications / 10为127.0.0.1 21:47:15 -0500 
处理NotificationsController#destroy as JS
参数:{id=>10}
SQL(0.4ms)SELECT name
FROM sqlite_master
WHERE type ='table'AND NOT name ='sqlite_sequence'
SQL(0.3ms)SELECT name
FROM sqlite_master
WHERE type ='table'AND NOT name = 'sqlite_sequence'

用户加载(0.7ms)SELECTusers。* FROMusersWHEREusers。id= 1 LIMIT 1
插入负载(0.4ms)SELECT slugs* FROMslugsWHERE(slugs.sluggable_id = 1 ANDslugs.sluggable_type ='User')OR DER BY id DESC LIMIT 1
**** AREL(0.3ms)UPDATEusersSETremember_token= NULL,remember_created_at= NULL,updated_at='2011-06-22 02:47: 15.913839',preferences='---
:email_notifications:''true''
'WHEREusers。id= 1
通知加载(0.2ms)SELECT通知* FROM通知WHERE通知id= 10 LIMIT 1
用户负载(1.0ms)SELECTusers* FROMusersWHEREusersid= 1 LIMIT 1
AREL(0.3ms)更新usersSETnotifications_count= COALESCE(notifications_count,0) - 1 WHEREusers。id= 1
AREL(0.1ms) FROMnotificationsWHEREnotifications。id= 10
渲染通知/ destroy.js.erb(0.7ms)
在6416ms完成200 OK(浏览次数:9.6ms | ActiveRecord:4.1ms)

所以,它是,它看起来像用户表的一部分正在获得设置为null,特别是我怀疑的remember_token触发Devise结束会话,或者也可能在Devise会话被破坏之后完成。但是如何跟踪这一点?



我可以想到的唯一的事情会导致通知与用户交互是有一个 counter_cache 用户 notifications_count



我欣赏如何调试的想法和建议! >

- 编辑3 -



在使用ruby-debug挖掘之后,它看起来像是与Devise和更改相关的问题到rails.js脚本。请参阅:



https:// github .com / plataformatec / devise / issues / 913



https://github.com/ryanb/cancan/issues/280



我正在尝试一些建议在这些线程上,如果我找到一个解决方案将发布。

解决方案

原来,这与更改Rails jQuery UJS司机和设计师。我更新了Devise而不更新jQuery UJS - 而Devise正在期待CSRF令牌的处理方式不同,因此正在将ajax请求处理为未经授权,这意味着破坏当前用户的会话。升级到最新的jQuery Rails驱动程序解决了这个问题。


The title pretty much explains it. I'm having an odd situation where views that allow users to delete notifications using Ajax cause the current_user to be logged out. I don't even know where to begin debugging this...

Here's the controller

class NotificationsController < ApplicationController

    def destroy
        @notification = Notification.find(params[:id])
        @notification.destroy
        respond_to do |format|
            format.js
        end
    end


end

This is the entire controller, nothing is abridged. The notifications are generated by the system, so the only action a user can take is to "dismiss" (ie. delete) them.

I also tried this using the newer respond_with syntax and had the same effect.

I'm using Devise, and Rails 3.0.9. Any idea what could be going on -- or suggestions on how to debug??

-- EDIT 1 --

Routes.rb

resources :notifications, :only => [:destroy]

Delete link

%span.delete= link_to( 'dismiss', notification_path(notification), :method => :delete, :remote => true )

-- EDIT 2 --

Well, I noticed something new in the logs -- see **** below.

Started DELETE "/notifications/10" for 127.0.0.1 at 2011-06-21 21:47:15 -0500
  Processing by NotificationsController#destroy as JS
  Parameters: {"id"=>"10"}
  SQL (0.4ms)   SELECT name
 FROM sqlite_master
 WHERE type = 'table' AND NOT name = 'sqlite_sequence'
  SQL (0.3ms)   SELECT name
 FROM sqlite_master
 WHERE type = 'table' AND NOT name = 'sqlite_sequence'

  User Load (0.7ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
  Slug Load (0.4ms)  SELECT "slugs".* FROM "slugs" WHERE ("slugs".sluggable_id = 1 AND "slugs".sluggable_type = 'User') ORDER BY id DESC LIMIT 1
  ****AREL (0.3ms)  UPDATE "users" SET "remember_token" = NULL, "remember_created_at" = NULL, "updated_at" = '2011-06-22 02:47:15.913839', "preferences" = '---
:email_notifications: ''true''
' WHERE "users"."id" = 1
  Notification Load (0.2ms)  SELECT "notifications".* FROM "notifications" WHERE "notifications"."id" = 10 LIMIT 1
  User Load (1.0ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
  AREL (0.3ms)  UPDATE "users" SET "notifications_count" = COALESCE("notifications_count", 0) - 1 WHERE "users"."id" = 1
  AREL (0.1ms)  DELETE FROM "notifications" WHERE "notifications"."id" = 10
Rendered notifications/destroy.js.erb (0.7ms)
Completed 200 OK in 6416ms (Views: 9.6ms | ActiveRecord: 4.1ms)

So, there it is, it looks like part of the users table is getting set to null, particularly the remember_token which I suspect is triggering Devise to end the session, or maybe this is done by Devise after the session is destroyed. But how do I track that down?

The only thing I can think of that causes notifications to interact with Users is there's a counter_cache on Users for notifications_count.

I appreciate thoughts and suggestions on how to debug!

-- EDIT 3 --

After digging with ruby-debug it looks like the issue is related to Devise and changes to the rails.js script. See:

https://github.com/plataformatec/devise/issues/913

https://github.com/ryanb/cancan/issues/280

I'm trying out some of the suggestions on those threads and will post if I find a solution.

解决方案

It turns out this had to do with changes to the Rails jQuery UJS driver and Devise. I had updated Devise without updating jQuery UJS -- and Devise was expecting the CSRF token to be handled differently, so it was processing the ajax request as unauthorized which meant destroying the current user's session. Upgrading to the latest jQuery Rails driver fixed the problem.

这篇关于Ajax删除链接注销current_user的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆