Code Updated
This is my js
destroy.js.erb
$('.delete_loop').on('ajax:success', function () {
$(this).closest('tr').fadeOut();
var loopNumber = $('.form-loop-number').html()
$('.form-loop-number').html(loopNumber - 1)
});
my ajax is called with rails :remote => true helper
index.html.erb:
= link_to calendar_video_ad_schedule_path(schedule.id), method: :delete, :remote => true, data: { confirm: 'Êtes vous sûr ?' }, :class => 'delete_loop' do
i.icon-trash style='color:grey; font-size: 1.2em'
When I delete an item I want the number of my .form-loop-number in the html to decrease. It works the 1st time, but if I call the method twice or more, the number decreases more than once.
1st time I remove item: loopNumber - 1
2nd time I remove item: loopNumber - 2
3rd time I remove item: loopNumber - 3
etc...
My controller responds sur js format
controller:
def destroy
@schedule = Schedule::VideoAd.find(params[:id])
@schedule.destroy
respond_to do |format|
format.html
format.js
end
end
I only want it to decrease 1 each time. How can I do this ?
$('.delete_loop').on('ajax:success', function () {...});
located?