Save form values between submits

Hello all. I need to save select tag values between form submits. There is the code snippet i have in my erb file:

  <%= form_tag(companies_path, { method: 'get' , id: 'select_type_form'}) do %>
    <%= select_tag 'company_type',
                options_from_collection_for_select(@company_types, 'id', 'name'),
                onchange: "$('#select_type_form').submit();" %>
  <% end %>

When user selects one of the values, an the page is being submitted and html table change its contents. Unfortunately after a form submit, select tag returns to its initial value, but i want it to display the value user has chosen before submit. Thank you.

1 Like

I think, I’ve found the solution - I added fourth arg to options_from_collection_for_select which is taken from the request params. Not sure if it is the right way, but it’s working:

<%= select_tag 'company_type',
            options_from_collection_for_select(@company_types, 'id', 'name', params[:company_type]),
            onchange: "$('#select_type_form').submit();" %>
1 Like

One thing I’ve experienced with Rails is that you can’t have forms overlap each other. For example if you have a bunch of rows with check boxes to select each row item… if that is implemented as a form, then you can’t have another form in it. I think that has to do with Rails deciphering which form is which. One solution is to write a JavaScript alternative to allow intermingling submission actions within a form.

I’ve written a gem that may interest you. Dynaspan; with one method you can have a field that updates with the Rails server instantly upon any change. Currently permits a input field, text box, and drop-down option. It does it in the background so your page doesn’t reload and it leaves any form in the page untouched.

1 Like

That’s not a Rails limitation. It is an HTML one: “There can be several forms in a single document, but the FORM element can’t be nested.”.

2 Likes