I have the following simple_form_for
with date selectors to filter Invoices by due date.
When the user inputs a start_date
and end_date
and submits to filter the invoices, the page reloads and the form fields are reset (the "pick a date" placeholder shows).
How can I reference those field's values so when the page reloads their values are the ones previously selected? (they are in the params
now).
I tried adding a value: f.object.start_date
option but it doesn't seem to work.
I also tried value: params[:date_params][:start_date]
to no success either.
<div class="col-md-6 my-2" >
<%= simple_form_for :date_params, { url: admin_invoices_batch_edit_index_path, method: :get } do |f| %>
<%= f.input :start_date, as: :string, placeholder: "Pick a start date",
input_html: { data:{ controller: "flatpickr",
attributes:{ enableTime: false, enableSeconds: false } } } %>
<%= f.input :end_date, as: :string, placeholder: "Pick an end date",
input_html: { data:{ controller: "flatpickr",
attributes:{ enableTime: false, enableSeconds: false } } } %>
<%= f.button :submit, "Filter" %>
<% end %>
</div>
In your controller action, grab the parameter values.
@start_date = params.has_key?(:date_params) ? params[:date_params][:start_date] : nil
@end_date = params.has_key?(:date_params) ? params[:date_params][:end_date] : nil
In your view add the value to the input_html
for each param
input_html: { value: @start_date ...
This will set the parameters when available or leave them as nil when not.