Hi,
I have a button which sends an ajax request, see below:
<%= link_to "Save", {:controller => 'budgets', :action => 'validate', :id => @budget}, :class => "button post" %>
The jquery js is below, i basically attach the xhr request to any link
with a class of “post” which is above. This all works fine in firefox
but not in safari or chrome.
/*
Post data via html
Use the class post in your link declaration
<%= link_to ‘My link’, my_new_path(),:class => “post” %>
*/
jQuery.fn.postWithAjax = function() {
this.unbind(‘click’, false);
this.click(function() {
$.post($(this).attr(“href”), $(this).serialize(), null, “script”);
return false;
})
return this;
};
function ajaxLinks(){
$(‘a.post’).postWithAjax();
}
$(document).ready(function() {
// All non-GET requests will add the authenticity token
$(document).ajaxSend(function(event, request, settings) {
if (typeof(window.AUTH_TOKEN) == “undefined”) return;
// IE6 fix for http://dev.jquery.com/ticket/3155
if (settings.type == ‘GET’ || settings.type == ‘get’) return;
settings.data = settings.data || "";
settings.data += (settings.data ? "&" : "") +
“authenticity_token=” + encodeURIComponent(window.AUTH_TOKEN);
});
ajaxLinks();
});
I get the following error message in the logs when trying to click the
link in safari.
[FATAL: 1105 16:32:21 #4899] /!\ FAILSAFE /!\ Fri Nov 05 16:32:21 +0000
2010
Status: 500 Internal Server Error
You have a nil object when you didn’t expect it!
You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating nil.attributes
/opt/local/lib/ruby/gems/1.8/gems/activesupport-2.1.0/lib/active_support/vendor/xml-simple-1.0.11/xmlsimple.rb:708:in
get_attributes' /opt/local/lib/ruby/gems/1.8/gems/activesupport-2.1.0/lib/active_support/vendor/xml-simple-1.0.11/xmlsimple.rb:462:in
collapse’
/opt/local/lib/ruby/gems/1.8/gems/activesupport-2.1.0/lib/active_support/core_ext/hash/conversions.rb:13:in
xml_in_string' /opt/local/lib/ruby/gems/1.8/gems/activesupport-2.1.0/lib/active_support/core_ext/hash/conversions.rb:23:in
xml_in_string’
/opt/local/lib/ruby/gems/1.8/gems/activesupport-2.1.0/lib/active_support/core_ext/hash/conversions.rb:171:in
`from_xml’
My own personal hunch from googling around is its something to do with
the authentiaction token but nothing i have tried has fixed this.
Has anyone got any idea what is wrong?
JB