Let's say we have a simple search form that submits a GET request for searching/filtering:
<%= form_with(scope: :search, url: url_for, local: false, method: :get) do |f| %>
<%= f.text_field :title %>
<% end %>
When we use form_with (or the old form_for) with an ActiveRecord object, all fields get automatically pre-filled with the current value of the AR object attribute.
But in the example above, if we use only a scope (in our case, search), that doesn't happen when we open the page with a URL like so:
http://localhost:3000/blog?search%5title%5D=test
Even tough that URL will make rails populate params[:search] with {title: 'test'} for us, it would still require to pass the value manually for each field, like so:
<%= f.text_field :title, value: params.dig(:search, :title) %>
This gets very tedious and error-prone as the search form grows and seems such a common requirement that I can't believe there isn't a more 'rails way' of doing this without resorting to something like the Form Objects Design Pattern.
Isn't there any automatic way of pre-filling those inputs without having to pass the values manually from params?