标题几乎解释了这一点。我遇到了一种奇怪的情况,允许用户使用 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 --
路线.rb
resources :notifications, :only => [:destroy]
删除链接
%span.delete= link_to( 'dismiss', notification_path(notification), :method => :delete, :remote => true )
-- 编辑 2 --
好吧,我在日志中注意到了一些新内容——请参阅下面的****。
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)
所以,看起来用户表的一部分被设置为空,特别是我怀疑正在触发 Devise 结束会话的 Remember_token,或者这可能是在会话被销毁后由 Devise 完成的。但我该如何追踪呢?
我能想到的唯一导致通知与用户交互的事情是有一个counter_cache
关于用户notifications_count
.
我很欣赏有关如何调试的想法和建议!
-- 编辑 3 --
使用 ruby-debug 进行挖掘后,看起来问题与 Devise 和对rails.js 脚本的更改有关。看:
https://github.com/plataformatec/devise/issues/913 https://github.com/plataformatec/devise/issues/913
https://github.com/ryanb/cancan/issues/280 https://github.com/ryanb/cancan/issues/280
我正在尝试这些线程上的一些建议,如果找到解决方案,我会发布。