I have been scouring different resources to solve this problem, but I can't seem to figure out what the issue might be.
I am trying to do a ajax form processing with jquery.
My application.html.erb contains the following lines
<%= javascript_include_tag "http://code.jquery.com/jquery-1.6.1.js"%>
<%= javascript_include_tag "http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.12/jquery-ui.min.js" %>
<%= javascript_include_tag "jquery.rails.js" %>
jquery.rails.js
conta开发者_JS百科ins the latest from https://github.com/rails/jquery-ujs`
I have the typical form_for call: <%=form_for @data, :remote => true do |f| %>
Which trigger the create handler of my controller, since this is a ajax call I have the appropriate create.js.erb
which contains a jquery such a $("#data_table").html("something")
The whole process works nearly correctly: the ajax called is made correctly, and when I look at the browser developer tool, I see that it returns the correct XHR resource containing the $("#data_table").html("something")
but what it returns is not evaluated by the browser.
I have tried debugging the different scripts, but I couldn't get anywhere meaningful, but I could see that the data return was being process by my rails.jquery.js
and jquery.js
scripts.
I have found different information around and tried all of them without success. This should be reasonably trivial, but I can't figure it out.
I got it to work, but I have a hard time imagining this is the right way of doing this, but after many hours of trial and error (and even reading jquery.js code), this is the only one which ended up work. I am still hoping for a better answer.
I have added a binding to the form to execute upon ajax:complete
event.
So my page contains the following javascript now:
$(document).ready(
function(){
$('#form_id').bind('ajax:complete',function(evt,xhr,status){
eval(xhr.responseText);
});
)
So now when the ajax call is complete, it will evaluate the xhr.responseText.
The answer for this issue may be in here Rails 3 ajax response is not executed
The key for me was to disable the layout by adding
layout false
into the controller. Without this the ajax response contained also all the stuff I had in /views/layouts/application
精彩评论