0

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!

4
  • Can you show me your permitted params? Commented Sep 22, 2015 at 0:48
  • 3
    You can't have more than one input with the same name in an HTML form. That's just how HTML works. Commented Sep 22, 2015 at 0:58
  • @Zahid params.require(:member).permit(:name, :academic_attributes => [:id]) Commented Sep 22, 2015 at 1:03
  • 2
    If you have 2 fields with the same name, the last one's value will be used by Rails every time. If the both fields_for forms are exactly alike, why do you even have two of them? Why don't you just create one single form and use the same for both options Undergrad and Alumni? Commented Sep 25, 2015 at 14:21

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.