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?

Community
  • 1
  • 1
Ajay
  • 473
  • 7
  • 25

1 Answers1

0

Finally, i got it working. I was missing:

<%= csrf_meta_tags %>

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

Ajay
  • 473
  • 7
  • 25