And*_*rew 11 ajax ruby-on-rails devise ruby-on-rails-3
标题几乎解释了它.我有一个奇怪的情况,允许用户使用Ajax删除通知的视图导致current_user被注销.我甚至不知道从哪里开始调试这个......
这是控制器
class NotificationsController < ApplicationController
def destroy
@notification = Notification.find(params[:id])
@notification.destroy
respond_to do |format|
format.js
end
end
end
Run Code Online (Sandbox Code Playgroud)
这是整个控制器,没有任何删节.通知由系统生成,因此用户可以采取的唯一操作是"解雇"(即删除)它们.
我也尝试使用更新的respond_with语法并具有相同的效果.
我正在使用Devise和Rails 3.0.9.知道会发生什么 - 或者如何调试?
- 编辑1 -
的routes.rb
resources :notifications, :only => [:destroy]
Run Code Online (Sandbox Code Playgroud)
删除链接
%span.delete= link_to( 'dismiss', notification_path(notification), :method => :delete, :remote => true )
Run Code Online (Sandbox Code Playgroud)
- 编辑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)
Run Code Online (Sandbox Code Playgroud)
所以,就是这样,看起来像用户表的一部分被设置为null,特别是我怀疑正在触发Devise结束会话的remember_token,或者这可能是在会话被破坏后由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
我正在尝试关于这些线程的一些建议,如果我找到解决方案,我会发布.
Die*_*ngs 12
我遇到了类似的问题.解决方案就像添加一样简单
<%= csrf_meta_tag %>
到布局.
事实证明这与Rails jQuery UJS驱动程序和Devise的更改有关.我更新了Devise而没有更新jQuery UJS - 并且Devise期望CSRF令牌的处理方式不同,因此它将ajax请求处理为未授权,这意味着破坏了当前用户的会话.升级到最新的jQuery Rails驱动程序修复了该问题.
| 归档时间: |
|
| 查看次数: |
2553 次 |
| 最近记录: |