0

I has some features which settings I save in session. But after 1 times reload they reset and session values doesn't exist there.

First load I get session

{"session_id"=>"xxx"} 

After save value I get

{"session_id"=>"xxx", "value"=>"100"} 

And when I reload my page again I get reset session

{"session_id"=>"xxx"} 

Why it can be?

2
  • How are you manipulating the session? Commented Oct 12, 2013 at 17:58
  • add - session[:value] = 100 and remove - session[:value] = nil Commented Oct 12, 2013 at 18:02

3 Answers 3

1

Igor is exactly right. If that happens to be the case in your application, do something like this:

$(document).ajaxSend((event, jqxhr, settings) -> jqxhr.setRequestHeader("X-CSRF-Token", $('meta[name="csrf-token"]').attr('content')) jqxhr.setRequestHeader("X-CSRF-Param", $('meta[name="csrf-param"]').attr('content')) return ) 

Now that's CoffeScript, and that assumes you are using JQuery. Regardless, the point is you need to send the CSRF metadata along for the ride with your Ajax requests.

Sign up to request clarification or add additional context in comments.

Comments

1

Most likely you're not passing the CSRF token in one of your (probably AJAX) requests. If rails receives invalid CSRF token - it resets the session.

Check out: http://guides.rubyonrails.org/security.html#cross-site-request-forgery-csrf

Update: Vidya is right. You may also want to add the following code to your ApplicationController - to set the XSRF token for AJAX calls

after_filter :set_csrf_cookie def set_csrf_cookie cookies['XSRF-TOKEN'] = form_authenticity_token if protect_against_forgery? end 

1 Comment

I have <%= csrf_meta_tags %> in head tag. Does you mean that?
0

Thanks for every one, I find problem but I can't explain it. Later I remove row from js file app/assets/javascripts/application.js

//= require jquery_ujs 

When I put it back, it's stop reset session values.

1 Comment

Cool! Didn't know jquery_ujs handles the CSRF token (which is logical btw). Don't forget to accept your own answer, so that others could know what worked for you.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.