1

Rails 3.2.8とajaxコールバックに問題があります...それらはまったく起動していないようです...Jqueryコード(chromeツールボックスでjsスクリプトを見ることができます)でバインドしようとしましたが、 link_to行に:success =>'alert( "bla")'を配置しようとしましたが、それでも何も実行されません...私の行が実際に削除されているため、コントローラーは機能します...しかしバインドできませんコールバックに!助けてください!

これが私の見解です:

<td><%= link_to 'Destroy', pet, :method => :delete, :remote=>true, :class => 'delete_pet' %></td>

これが私のコントローラーのアクションです

def destroy
    @pet = Pet.find(params[:id])
    @pet.destroy

    respond_to do |format|
      format.html { redirect_to(pets_url) }
      format.js { render :nothing => true }
    end
  end

これが私のjsコードです:

jQuery(function($) {
    $('.delete_pet').bind("ajax:before", function(){alert('bla2');})
                    .bind("ajax:success", function(){alert('bla2');})
                    .bind("ajax:failure", function(){alert('bla2');})
                    .bind("ajax:complete", function(){alert('bla2');});
});
4

3 に答える 3

0

問題は、私のapplication.jsに手動でjqueryファイルを含めていたということでした...このように。

//= require jquery-ui-1.8.23.custom

恥ずかしい!

于 2012-09-07T01:51:41.687 に答える
0

交換

 format.js { render :nothing => true }

 format.json { render :nothing => true }

また、置き換えます

<%= link_to 'Destroy', pet, :method => :delete, :remote=>true, :class => 'delete_pet' %>

<%= link_to 'Destroy', pet, :method => :delete, :remote=>true, 'data-type'=>'json', :class => 'delete_pet' %>
于 2012-09-06T23:46:18.727 に答える
0

私はrespond_toブロックで同様の問題を抱えています。をチェックして一時的に回避しましたrequest。たとえば、コードは次のようになります。

def destroy
  @pet = Pet.find(params[:id])
  @pet.destroy

  if request.xhr? 
    # the request was received via an AJAX request
    render :nothing => true
  else
    redirect_to(pets_url)
  end
end

ブロックほどエレガントではありませんresond_toが、期待どおりに確実に機能するようです。

幸運を!

于 2012-09-07T01:02:01.287 に答える