我有一个场地模型,每个场地属于一个区域和一个类型。我在索引页面上有两个由复选框组成的表单,允许我过滤它们所属的类型或区域所显示的场地记录。但是,只使用了def索引中的最后一行,我如何组合它们才能使两个表单都处于活动状态?
控制器:
class VenuesController < ApplicationController
def index
@venues = Venue.all(:conditions => {:area_id => params[:areas]})
@venues = Venue.all(:conditions => {:venuetype_id => params[:venuetypes]})
end
end
查看:
<div class="filter_options_container">
<form class="filter_by_area_form", method="get">
<% Area.all.each do |a| %>
<%= check_box_tag("areas[]", a.id) %>
<%= a.name %>
<% end %>
<input type="submit" value="Filter" />
</form>
<br><br>
<form class="filter_by_venuetype_form", method="get">
<% Venuetype.all.each do |v| %>
<%= check_box_tag("venuetypes[]", v.id) %>
<%= v.name %>
<% end %>
<input type="submit" value="Filter" />
</form>
</div>
<div class="venue_partials_container">
<%= render :partial => 'venue', :collection => @venues %>
<div class="clearall"></div>
<%= link_to 'add a new venue', new_venue_path, :class => "add_new_venue_button" %>
</div>
因此,只有venuetype表单有效,或者如果我将def索引更改为:
def index
@venues = Venue.all(:conditions => {:venuetype_id => params[:venuetypes]})
@venues = Venue.all(:conditions => {:area_id => params[:areas]})
end
然后只有区域才有效。
修改
我已将控制器更改为:
def index
@venues = Venue.all(:conditions => {:venuetype_id => params[:venuetypes]}&&{:area_id => params[:areas]})
end
和观点:
<div class="filter_options_container">
<form class="filter_form", method="get">
<fieldset class="filter_form_fieldset">
<% Area.all.each do |a| %>
<%= check_box_tag("areas[]", a.id) %>
<%= a.name %>
<% end %>
</fieldset><br>
<fieldset class="filter_form_fieldset">
<% Venuetype.all.each do |v| %>
<%= check_box_tag("venuetypes[]", v.id) %>
<%= v.name %>
<% end %>
<input type="submit" value="Filter" />
</fieldset>
</form>
</div>
从表单中选择区域和类型并单击提交按钮后,URL显示“/ venues?areas [] = 2&amp; venuetypes [] = 13”,但仍然只按区域过滤结果而不是类型。
感谢您的帮助。
答案 0 :(得分:2)
要构建搜索查询,您可以使用scoped
:
Rails 2
@venues = Venue.scoped
@venues = @venues.scoped(:conditions => { :venuetype_id => params[:venuetypes] }) if params[:venuetypes].present?
@venues = @venues.scoped(:conditions => { :area_id => params[:areas] }) if params[:areas].present?
如果您创建了像@Steve Smith建议的命名范围:
class Venue < ActiveRecord::Base
named_scope :with_type, lambda { |types|
types.present? ? { :conditions => { :venuetype_id => types } } : {} }
named_scope :with_area, lambda { |areas|
areas.present? ? { :conditions => { :area_id => areas } } : {} }
end
然后你就可以做到:
@venues = Venue.with_type(params[:venuetypes]).with_area(params[:areas])
Rails 3
@venues = Venue.scoped
@venues = @venues.where(:venuetype_id => params[:venuetypes]) if params[:venuetypes].present?
@venues = @venues.where(:area_id => params[:areas]) if params[:areas].present?
或者:
class Venue < ActiveRecord::Base
scope :with_type, lambda { |types|
types.present? ? where(:venuetype_id => types) : scoped }
scope :with_area, lambda { |areas|
areas.present? ? where(:area_id => areas) : scoped }
end
然后你就可以做到:
@venues = Venue.with_type(params[:venuetypes]).with_area(params[:areas])