2012-12-19 2 views
6

Ruby on Railでは、2つの部分データを更新しようとしています。イベントが発生しているrender partialの応答が機能していない

show.html.erb:

<div id="filters"><%= render :partial => "pricelistfilters" %></div> 

pricelistfilters.html.erb:

<% @properties.each do |prop| %> 
    #Render the page on properties (and the newproperties) 
    #<select ...><option>...</option><option>...</option> 
    #</select> 
<% end %> 

products.js - レンダリングされた部分

ため>イベント
$(window).ready(function(){ 
    selectionchanges(); 
}); 
function selectionchanges(){ 
    $('#filters select').change(function(){ 
    //Doing stuff to send with ajax 

    //Ajax: 
    $.ajax({ 
     url: document.URL, 
     type: 'POST', 
     data: params 
    }); 
    }); 
} 

products_controller.rb - 変更を処理するために>コードは私のレンダリングされたアイテムが完全に良いです

def show 
    #Changing the properties (and the pricelist properties) 
    @properties #is filled 
    @newproperties #is filled 
    respond_to do |format| 
    format.html 
    format.js 
    end 
end 

show.js.erb

$('#filters').html('<%= escape_javascript(render :partial => "pricelistfilters") %>'); 
selectionChanges(); 

を作りました。しかし、私がレンダリングしたアイテムで適切な応答を得たとき、それはもはやajaxを送信しません。だから私の選択したアイテムのイベントは消えてしまったのですが、私はそれらを "selectionchanges();"私のshow.js.erbファイルの最後に?

誰にでも解決策が見えますか?事前に

ご挨拶と感謝

+0

コードブロックが異常に動作していました。ありがとうございました。 – ReBa

+0

ようこそ。私はあなたの問題に対する良い答えを見つけることを願っています。 – jdl

+3

あなたは 'selectionchanges'と 'selectionChanges'を持っています、それはタイプミスですか? – RadBrad

答えて

2

怒鳴るよう、product.jsにjQueryのライブ機能を使用してみてください:基本的に

$(window).ready(function(){ 
    selectionchanges(); 
}); 
function selectionchanges(){ 
    $('#filters select').live('change', function(){ 
    //Doing stuff to send with ajax 

    //Ajax: 
    $.ajax({ 
     url: document.URL, 
     type: 'POST', 
     data: params 
    }); 
    }); 
} 

は、あなたが最初のバインドHTML DOMの要素を交換していますイベント 'change' to。ライブでは、要素を置き換えても、jQueryはバインドを再実行します。

希望すると助かります!

+0

これを見てください。完璧に動作します!ありがとう。 – ReBa

関連する問題