3
在我的佈局文件中,我有兩個下拉菜單。當在一個(類別)中進行選擇時,第二個(項目)的內容需要更新。Rails 3.2.8中的AJAX應用程序未重新加載目標div
<div id="pull_down_items" class="pull-right">
<%= render :partial => 'layouts/pull_down_items' %>
</div>
<%= render :partial => 'layouts/pull_down_category' %>
佈局/ _pull_down_items.html.erb:
<%= select_tag "current_item", options_from_collection_for_select(current_category.items, "id", "item_name", session[:current_item]), :id=>"change_current_item", :remote => true %>
佈局/ _pull_down_category.html.erb:
<%= select_tag "current_category", options_from_collection_for_select(categories, "id", "category_name", session[:current_category]), :id=>"change_current_category", :remote => true %>
的jQuery中的application.js
在佈局文件:
$("#change_current_category").change(function() {
var category_id = $(this).val();
var url = "/system/" + category_id + "/change_category";
$.post(url, category_id, function(html) {
});
});
在我system_controller:
def change_category
unless params["id"].blank?
session[:current_category] = params["id"].to_i
current_category.reload # this is a method in application_controller.rb
end
respond_to do |format|
format.js
end
end
在change_team.js.erb:
$("#pull_down_items").html("<%= render :partial => 'layouts/pull_down_items' %>")
結果:會話[:current_category]更改爲正確的類別。項目下拉菜單div不重新加載。
感謝您的幫助。