Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ajax beforeSend works only once on Rails

I have 3 files:

index.html.erb is my homepage

inside the index I render a partial, _relationship.html.erb, and there is a form with the following line:

<%= form_tag('/relationships/update/', :remote => true, :id => "relationships_form_#{@count}") do %>

i also have index.js.erb (relationship.html is in index.html.erb)

jQuery("#relationships_form_1").bind("ajax:success", function() {
  $("#box_relationships").hide().html("<%=escape_javascript(render('relationships/relationships'))%>").fadeIn(2000, 'swing');
  })

the form is sent every time, and the value is changed.

i want to show a loading gif using the beforeSend option.

I tried (on index.html.erb. if i put it in the _relationships partial, it doesn't work at all):

jQuery("#relationships_form_1").bing("ajax:beforeSend", function() {
$("#relationship_save").show();
});  

But it works only once.

Then, i added the before rails option

:before => '$("#relationship_save").show();'

It works, again, only once.

I even tried the .on on jquery

    jQuery("#relationships_form_1").on("ajax:beforeSend", function() {
$("#relationship_save").show();
});

And it still works only once.

The form is sent every time, the value is stored every time, the beforesend works only once

Any thoughts?

like image 777
wachichornia Avatar asked Dec 27 '22 02:12

wachichornia


1 Answers

jQuery(document).on("ajax:beforeSend", "#relationships_form_1", function() {
  $("#relationship_save").show();
});

Because your ajax-loaded form is not the same as initial form.

like image 136
mikdiet Avatar answered Jan 09 '23 01:01

mikdiet