JQuery + Rails + ajaxForm + js.erb problem
I have a problem that is driving me crazy, and according to everything I've seen and read on the internet, it should work fine.
I am using jQuery with my Rails app instead of prototype and I am using the ajaxForm plugin to submit some form data via ajax. Form data is received by fines, the corresponding controller action is called "fine", the corresponding response_to block for the js type is called "fine".
The problem is that the contents of the * .js.erb file are returned to the client browser and are not interpreted as javascript. Nothing happens to this. Yes, I set dataType: 'script' but it still doesn't work. I also haven't tried using the ajaxForm plugin and manually using the $ .post function on the submit form, but still the same result.
for some code examples, here is my ajaxForm call:
$("#purchase-item-form").ajaxForm({
dataType: 'script'
});
I am also setting the accepts header correctly, this works great because my format.js block is the one that is being executed
jQuery.ajaxSetup({
'beforeSend': function(xhr) { xhr.setRequestHeader("Accept", "text/javascript") }
});
in my action * .js.erb file all I have is an alert statement for now, to help debug the problem until I can figure it out:
alert("action processed successfully");
It just doesn't work in firefox or safari. If I look in the firebug console I can see that the warning statement is being returned as a response from the ajax call, it just isn't being evaluated by the client as javascript.
FYI, I am running Rails 2.3.4 and jQuery 1.3.2 if it matters
source to share
I am facing EXACT problem . You need to manually eval
return the code. I can't remember exactly why it doesn't work around, but I used the same plugins as you and that fixed it.
Edit 1: Read the article as it helped to fix the problem.
Edit 2: Try this:
$("#purchase-item-form").ajaxForm({
dataType: 'script',
success: evalResponse
});
function evalResponse(responseText, statusText) {
eval(responseText);
}
Take a look here for more information.
source to share