这应该相对简单,我可能会过度思考,但我无法弄明白。是时候打电话给S.O.在寻求帮助。首先,我想要完成的项目符号列表,然后是代码。
好的,现在是灾难的时候 - 代码。
场地管理员
def index
case
when params[:area]
@venues = Venue.where(:area => params[:area])
when params[:day]
@venues = Venue.where(:day => params[:day])
end
end
application.html.erb
<%= form_tag(venues_path, :method=>:get, id: "search-form") do |f| %>
<div class="col span_1_of_3">
<%= select_tag "area", options_for_select([ " ", "Area 1", "Area 2" ], params[:area]), {:class=>"styled-select"} %>
</div>
<div class="col span_1_of_3">
<%= select_tag "day", options_for_select([ " ", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"], params[:day]), {:class=>"styled-select"} %>
</div>
<div class="col span_1_of_3">
<%= select_tag "day", options_for_select([[" ", " "], ["3pm", "t1500"], ["4pm", "t1600"], ["5pm", "t1700"], ["6pm", "t1800"], ["7pm", "t1900"], ["8pm", "t2000"], ["9pm", "t2100"], ["10pm", "t2200"], ["11pm", "t2300"], ["12am", "t0000"], ["1am", "t0100"], ["2am", "t0200"], ["3am", "t0300"], ["4am", "t0400"], ["5am", "t0500"], ["6am", "t0600"], ["7am", "t0700"], ["8am", "t0800"], ["9am", "t0900"], ["10am", "t1000"], ["11am", "t1100"], ["12pm", "t1200"], ["1pm", "t1300"], ["2pm", "t1400"]]), {:class=>"styled-select", id: "t", name: "t"} %>
</div>
<br>
<button data-style="expand-right" data-size="s" type=submit class="button">Submit</button>
<% end %>
场地模型(省略,因为除了为新场地添加嵌套属性之外我还没有真正修改它 - 这是有效的)。如果需要,我会发布它。
就像我说的那样,我一直在绞尽脑汁,我无法弄清楚如何构建正确的查询/过滤器以获得结果。我只是接近这一切都错了吗?
答案 0 :(得分:0)
您需要查看模型范围并编写如下内容:
的Gemfile:
gem 'has_scope'
型号:
scope :area, -> q { where area: q }
scope :day, -> q { where day: q }
在你的控制器中:
has_scope :area, :day
def index
@venues = apply_scopes(Venue)
end
Haven没有对它进行过测试,但是对于理智的范围过滤器你需要这样的东西。