Ajax Remove links from current_user log

The name pretty much explains this. I had a strange situation where views that allow users to delete notifications using Ajax cause the current_user file to exit. I don’t even know where to start this debugging ...

Here is 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 reduced. Notifications are generated by the system, so the only action the user can take is β€œtermination” (ie, Deletion).

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

I am using Devise and Rails 3.0.9. Any idea what might go 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 magazines - 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, here it is, it looks like part of the user table is getting null, especially mem_stoken, which I suspect is launching Devise to end the session, or maybe this is done by Devise after the session ends. But how can I track this?

The only thing I can think of about what causes user interaction is counter_cache for users for notifications_count .

I appreciate thoughts and suggestions on how to debug!

- EDIT 3 -

After digging ruby-debug, it looks like the problem is with Devise and changes to the rails.js script. Cm:

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

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

I will try some suggestions on these topics and publish if I find a solution.

+11
ajax ruby-on-rails ruby-on-rails-3 devise
Jun 22 2018-11-22T00:
source share
4 answers

It turns out that this is due to changes to the Rails jQuery UJS and Devise driver. I updated Devise without updating jQuery UJS - and Devise expected the CSRF token to be handled differently, so it treated the ajax request as unauthorized, which meant destroying the current user session. Updating to the latest jQuery Rails driver fixed the problem.

+9
Jun 28 2018-11-11T00:
source share

I had a similar problem. The solution was as simple as adding

<%= csrf_meta_tag %>

to the layout.

+12
Jul 22 '11 at 12:57
source share

Are you sure that this controller and action are the ones that are triggered by the request? It looks like the path you are DELETEING is wrong, and instead you are using your session path.

0
Jun 22 2018-11-11T00:
source share

Is it possible that the destruction notification path is redirected to the session destruction path through JS?

Do you have a destroy.js template in your notification views? try adding one that is empty, see if you get a different result.

0
Jun 22 2018-11-22T00:
source share



All Articles