我正在尝试建立一个页面,用户在调用模型Entreprise时可以将:city和:sector过滤器组合在一起。当我只有:city时,设置运行良好,但是当添加:sector时,页面不再更新。请问我做错了什么?有谁知道如何组合这些过滤器?
我的索引页面
<div class="col-md-12">
<%= form_tag events_path, method: 'get', id: "events_search" do %>
<% @cities = Event.all.distinct.pluck(:city) - ["", nil] %>
Filter by cities:
<% @cities.each do |city| %>
<div class="btn btn-default">
<%= check_box_tag "by_cities[]", city %> <%= city %>
</div>
<% end %>
<% @sectors = Event.all.distinct.pluck(:sector) - ["", nil] %>
Filter by sector:
<% @sectors.each do |sector| %>
<div class="btn btn-default">
<%= check_box_tag "by_sectors[]", sector %> <%= sector %>
</div>
<% end %>
<% end %>
<br>
</div>
</div>
<div id="events"><%= render 'principal' %> </div>
</div>
<script type="text/javascript">
$(function () {
$('input[type=checkbox]').change(function () {
$.get($('#events_search').attr('action'),
$('#events_search').serialize(), null, 'script');
return false;
});
$('#events_search').submit(function () {
$.get(this.action, $(this).serialize(), null, 'script');
return false;
});
});
</script>
我的控制器看起来像:
class EventsController < ApplicationController
before_action :authenticate_user!
has_scope :by_cities, type: :array
has_scope :by_sectors, type: :array
def index
@events = apply_scopes(Event).all.order("created_at DESC")
end
我的模型:
scope:by_cities,->城市{where(:city => city)} 范围:by_sectors,->部门{where(:sector =>部门)}
index.js.erb
$('#events').html('<%= escape_javascript(render('principal')) %>')