我正在尝试实现过滤器。我有一个带列的模型,status
我已经为该列定义了枚举状态以使其可读。而这些状态是固定的。
模型
class Production < ApplicationRecord
enum status:{
Preproduction:1,
Postproduction: 2,
Completed:3
}
end
我正在尝试根据用户给出的状态过滤表格。仅当用户输入状态的完整名称(如 )时才会过滤结果preproduction
。控制器如下:
控制器
def index
if params[:filter]
@productions = Production.where('productions.status like ?', "%#{Production.statuses[params[:filter].capitalize]}%")
else
@productions = Production.all
end
end
如您所见,我正在使用 params 进行查询。
我当前的表单标签看起来像这样
指数
<%= form_tag productions_path, :method => 'get', :id=> "contacts_search" do %>
<p>
<%= text_field_tag :filter, params[:filter] %>
<%= submit_tag "Filter", :name => nil %>
</p>
<div id="cresults_div"><%= render 'cresults'%> </div>
<% end %>
我有一些固定状态如下:
Production.statuses => {"Preproduction"=>1, "Postproduction"=>2, "Completed"=>3}
由于我在 params 的帮助下查询控制器,因此我无法f.select
像某些答案中建议的那样直接使用。如何Preproduction
Postproduction
Completed
在此文本字段的广告下拉列表中获取这些关键字?
<%= select_tag :'filter', options_for_select(Production.statuses, params[:filter]), include_blank: '--Select One Option--' %>
在控制器中: -
def index
if params[:filter]
@productions = Production.where(status: params[:filter])
else
@productions = Production.all
end
end
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句