Creating a rail form to filter an index page?

Guys from G'day, I had a problem with filtering the presentation of several thousand items of trade that I have in my system. According to the specifications of the system we are building, we must have a form that allows people to enter a start date and then an interval in minutes to filter the presentation of the elements. I built my helper functions to return all transactions during this interval, but I can’t create a form for my whole life that returns the dateTime value and integer value at the top of the index page?

Any ideas? Should I create a separate model object for assigning values ​​or is there an easier way?

+5
source share
2 answers

On the index page, you can create a filter form similar to this

<%= form_tag '', :method => :get do %>
  <%= text_field_tag :value_one %>
  <%= text_field_tag :value_two %>
  <%= submit_tag 'Filter' %>
<% end %>

Your form will then do a GET on your page with query string parameters, and then in your controller you will be able to search for filter parameters that could be passed.

+16
source

I like to gradually create a filter in my controller, i.e.

def index

  filter = []

  if params.has_key?("filter")

    if !params[:filter][:user_id].blank?
      id = params[:filter][:user_id]
      filter << ["user_id = #{id.to_i}"]
    end

    if !params[:filter][:project_id].blank?
      id = params[:filter][:project_id]
      filter << ["project_id = #{id.to_i}"]
    end

    if !params[:filter][:change_type_id].blank?
      id = params[:filter][:change_type_id]
      filter << ["change_type_id = #{id.to_i}"]
    end

  end

  @logs = LogEntry.where(filter.join(" AND ")).order('created_at DESC')    

end

If a filter is not selected in your view (I use a form helper select_tag), your database query returns all records.

+2
source

All Articles