0

I have ruby on rails site. The ApplicationController looks like

class ApplicationController < ActionController::Base
  protect_from_forgery
  before_filter :handle_cookie

   DEFAULT_MARKETPLACE = 1
  def handle_cookie
   if (session[:isImpersonation].nil?)
      puts "initilized to 0"
      session[:isImpersonation] = 0
   end
end

I am initializing the variable isImpersonation in session object if it is nil. When normal request is made (i.e non-ajax) the variable is initialized once once i.e. the session object retains the variable isImpersonation.

But when i make ajax call like:

    $.ajax({
    url : "/a/fetch",
    type : "POST",
    data : {
        runDate : $('#rundate_select :selected').val(),
        id : idVal,
        mp : mpVal
    },
    beforeSend : function(xhr) {
        xhr.setRequestHeader('X-CSRF-Token', $('meta[name="csrf-token"]')
                .attr('content'));
    },
    success : function(data) {
        var $elem = $('<div>').html(data);
        $("#overall").html(
                $elem.find('#overall').html());

    },
    error : function(data) {

    }
});

During this ajax call the variable in the session is re-initialized. I searched and followed the post: Rails not reloading session on ajax post.

But, its not working for me. Any idea what i am missing?

1
  • what does log say when ajax call is made?
    – Nithin
    Jun 24, 2014 at 13:22

1 Answer 1

0

Finally, i got it working. I was missing:

<%= csrf_meta_tags %>

in tag of my layout file i.e. application.html.erb

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.