I am trying to implement a filter. I have a model with column status
and I have defined enum status for that column inorder to make it readable. And these statuses are fixed.
model
class Production < ApplicationRecord
enum status:{
Preproduction:1,
Postproduction: 2,
Completed:3
}
end
And I am trying to filter the table based on the status given by the user.
The result is filtered only if the user enter the complete name of status like preproduction
. The controller is as follows:
controller
def index
if params[:filter]
@productions = Production.where('productions.status like ?', "%#{Production.statuses[params[:filter].capitalize]}%")
else
@productions = Production.all
end
end
As you can see I am using params to query.
My current form tag looks like this
index
<%= 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 %>
I have some fixed statuses as follows:
Production.statuses => {"Preproduction"=>1, "Postproduction"=>2, "Completed"=>3}
Since I am querying the controller with the help of params I cant directly use f.select
like suggested in some answers. How can I get these keywords Preproduction
Postproduction
Completed
in ad drop down of this text field ?