Setting a rails session in an ajax post
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?
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).
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).
精彩评论