Vinicius Martinson
Vinicius Martinson

Reputation: 95

Rails 4: multiple fields with same name not saving

I have a form that has fields with same name because of the "flow" of the form.

If the member is Undergrad:

<div id="if_undergrad">
    <%= f.fields_for :academic do |academic_full_degree| %>
        <%= academic_full_degree.text_field :major %>
    <% end %>
</div>

But, if the member is Alumni:

<div id="if_alumni">
    <%= f.fields_for :academic do |alumni| %>
        <%= alumni.text_field :major %>
    <% end %>
</div>

And I have a jQuery to show each div if the user selects alumni/undergrad from a drop-down.

If the member selects that he is Undergrad, Rails won't save the major into the database (I assume is because the major field of Alumni is blank).

Do you know how to make it work with the same name of fields?

Any help will be appreciated. Thank you!

Upvotes: 0

Views: 1222

Answers (1)

Cristian Oliveira
Cristian Oliveira

Reputation: 770

You can disable the fields that you don't want submit then they will not send to the backend.

Somenthing like that:

$("#if_alumni input[name*='major']").prop('disabled', true);

Upvotes: 1

Related Questions