Rails过滤器布尔属性

时间:2020-04-11 10:45:43

标签: ruby-on-rails ruby filtering

我需要在搜索表单中添加过滤器。我需要我的搜索可以过滤具有“玩具”或“高脚椅”的地方。如果用户添加过滤器之一(全部或都不添加),我会有点困惑如何在控制器中创建else条件。

index 0 : OperatingSystem
index 1 : 3
index 2 : Theory+Lab
index 3 : Prof.XYZ
index 4 : WebDevelopment
index 5 : 3
index 6 : OnlyLab 

1 个答案:

答案 0 :(得分:0)

我建议您使用ransack宝石,这样会更容易。

// View
<%= search_form_for @query, url: places_path, method: :get, class: "search-form" do |f| %>
  <%= f.label :high_chair_true %>
  <%= f.check_box :high_chair_true %>
  <%= f.label :high_chair_false %>
  <%= f.check_box :high_chair_false %>
  <%= f.label :toy_true %>
  <%= f.check_box :toy_true %>
  ...
  <%= f.submit "search" %>
<% end %>

// Controller
def index
  @query = Place.ransack(params[:query])
  @places = @query.result
end

玩组合来满足您的需求。