我的问题是我在一个页面中有太多的选择框,等待ajax响应非常慢。有效的方法是什么?感谢。
查看
<div class="item">
<%= collection_select :categorization, :classification_id, Classification.all, :id, :name %>
<%= select :categorization, :category_id %>
</div>
<div class="item">
<%= collection_select :categorization, :classification_id, Classification.all, :id, :name %>
<%= select :categorization, :category_id %>
</div>
#goes on...
的application.js
$("#categorization_classification_id").change( function() {
$.get("/get-categories", { classification_id: $(this).val() }, null, "script" );
});
控制器
def get_categories
@categories = Category.where("classification_id = ?", params[:classification_id])
respond_to do |format|
format.js
end
end
get_categories.js.erb
$("#categorization_category_id").html('<%= raw escape_javascript(options_for_select(@categories.map {|sc| [sc.name, sc.id]}).gsub(/\n/, '')) %>');