lundi 29 février 2016

Rails 3 Ajax-JQuery: view not updated when destroying an object

I encounter an issue when I destroy an object in AJAX with Rails 3.2.11.

I followed this tutorial: http://ift.tt/1DGQ2KV

The controller works fine, but in the view, nothing is done properly. I think my problem is JQuery, but I'm not sure, so where is my problem?

//views/tournaments/index.html.haml
%table.table.table-hover.table-bordered
 %thead
  %th Name
  %th Place
  %th Nb players max
  %th
  - if can? :update, @tournaments
   %th
  - if can? :destroy, @tournaments
   %th
 - @tournaments.each do |tournament|
  %tr
   %td= tournament.name
   %td= tournament.place
   %td= tournament.nb_players_max
   %td= link_to 'Show', tournament
   - if can? :update, @tournaments
    %td= link_to 'Edit', edit_tournament_path(tournament)
   - if can? :destroy, @tournaments
    %td= link_to 'Destroy', tournament, method: :delete, data: {confirm: 'Are you sure?'}, :class => 'destroy_tournament', :remote => true

My controller tournaments_controller.rb:

 def destroy
  @tournament = Tournament.find(params[:id])
  @tournament.destroy

  respond_to do |format|
   format.html { redirect_to tournaments_url }
   format.json { head :no_content }
   format.js   { render :layout => false }
  end
 end

destroy.js.erb:

$('.destroy_tournament').bind('ajax:success', function(){
 $(this).parent('tr').fadeOut(400, function(){
  $(this).remove();
 });
});

The console tells me that the controller works fine (200 OK), and in response, I have the content of the JS file.

What happened? :-(

Aucun commentaire:

Enregistrer un commentaire