问题
I'm crazy with multiple forms in the same page.
every working fine when there is one unique form and you can identify the unique id or unique class of this form or column...etc.
But my big problem come back when I have multiple forms in the same page. I want rendered the partial create_index when I create a new comment, in create.js.erb I have:
$("<%= escape_javascript(render 'create_index') %>").hide().prependTo(".comments_column").fadeIn(1500);
The problem is that if there are many columns above forms with the class .comments_column the comment its rendered in every columns with the same class.
A example in jquery is this:
http://jsfiddle.net/minitech/aC92Q/8/
For rails 3.1 don't working this example. If I put that javascript in create.js.erb. The first time don't show the comment created.
If I put only in create.js.erb:
$("<%= escape_javascript(render 'create_index') %>").hide().prependTo(".comments_column").fadeIn(1500);
rendered the partial in every column that contain the class .comments_column.
My question is How say to rails 3 the form or button that I am hit for render the partial only in this column?
回答1:
The problem its fixed!. For every world that have this problem:
Each rails UJS AJAX call provides six custom events that can be attached to:
ajax:before – right before ajax call
ajax:loading – before ajax call, but after XmlHttpRequest object is created)
ajax:success – successful ajax call
ajax:failure – failed ajax call
ajax:complete – completion of ajax call (after ajax:success and ajax:failure)
ajax:after – after ajax call is sent (note: not after it returns)
In My case I'll add an event listener to the ajax:success event on My submit button:
$('.comment_box form').bind('ajax:success', function() {
$("<%= escape_javascript(render(:partial => 'create_index'))%>").hide().prependTo($(this).parents(".comments_column").eq(0)).fadeIn(1500);
});
Its very very important call the custom events or the other side dont working fine.
来源:https://stackoverflow.com/questions/8717405/unobtrusive-javascript-rails-3-1-multiple-forms-same-page