【发布时间】:2017-11-30 22:03:17
【问题描述】:
我正在尝试基于列 status 对模型 Production 实施搜索/过滤操作。 status 列是整数类型。后来为了便于阅读,我在status 列上使用了枚举数据类型,如下所示。
class Production < ApplicationRecord
enum status:{
Preproduction:1,
Postproduction: 2,
Completed:3
}
end
然后我开始研究搜索/过滤功能,以根据用户给出的状态获取记录。
productions_controller
def filter
if params[:filter]
@productions = Production.where('productions.status like ?', "%#{params[:filter]}%")
else
@productions = Production.all
end
end
查看
<%= form_tag [:filter, :productions], :method => 'get' do %>
<p>
<%= text_field_tag :filter, params[:filter] %>
<%= submit_tag "Filter", :status => nil %>
</p>
<% end %>
现在,只有在文本字段中输入 1 2 或 3 之类的整数值时,我才能正确查询记录。当我输入像我分配的Preproduction 这样的状态时,我没有得到结果。我得到一个空白页。我怎样才能解决这个问题 ?如何让它接受字符串并成功查询?
【问题讨论】:
标签: ruby-on-rails