3

Kind of a strange problem, hopefully someone can help me out.

I want to set a session in my rails controller which I hit via jquery ajax post.

Its a regular rails form which i submit via ajax

 $.post($(this).attr('action'), $(this).serialize(), function(data){

 }, 'json');

which hits the leads controller

def create
    @lead = Lead.new(params[:lead])
    @lead.save!

    if @lead.save
      session[:lead] = "#{@lead.id}"
    end

  end

however the session doesnt seem to be set across the ajax call.

Anyone have an ideas on this?

user229044
  • 232,980
  • 40
  • 330
  • 338
stuartchaney
  • 432
  • 5
  • 16
  • Possibly related: http://stackoverflow.com/questions/5126721/rails-not-reloading-session-on-ajax-post – igorw Mar 25 '11 at 10:11

2 Answers2

0

Did you actually see create being called in your controller (did it appear in the log)?

BTW: you're calling save() twice, but this is not the problem (calling save multiple times returns true every time if the save is successful).

evaneykelen
  • 472
  • 3
  • 5
  • Yes, it is being shown in the logs, started POST "/leads" for 127.0.0.1 at 2010-12-02 21:23:24 +0000 Processing by LeadsController#create as JSON – stuartchaney Dec 02 '10 at 22:06
0

The problem could be because your using your localhost and not a proper domain I have heard some browsers ignore such sessions.

The other problem could be your not sending the CSRF authentication token used to stop forgery attacks, this is now a requirement for XHR requests by default and not just forms (although it may not have been with the version of rails at the time of your original post).