Enter data in rails before controller action

时间:2015-10-30 22:08:45

标签: ruby-on-rails-4

I have a transactions controller and view. When I click to link "Transactions", I show all transactions.

How to add a before display transactions such as enter start and end dates?

1 个答案:

答案 0 :(得分:0)

What you need is probably the ransack gem.

You can add a custom filtering mechanism in your controller like this:

def index
  @q = Transaction.ransack(params[:q])
  @transactions = @q.result(distinct: true)
end

and using in the view a simple for like this:

<%= search_form_for @q do |f| %>
  <%= f.label :start_date %>
  <%= f.search_field :created_at_gteq %>

  <%= f.label :end_date %>
  <%= f.search_field :created_at_lteq %>

  <%= f.submit %>
<% end %>

And this will let you filter the Transaction data using the created_at param as a filter. Modify the form accordingly if you need to filter using another Date/DateTime param from the model.