Questifer
Questifer

Reputation: 1123

Add number to name fields in rails form

I am looping over an array of objects which is creating multiple copies of the same form in my view. Each form has a hidden ID that responds to a particular comment. The forms are currently working, however to use front end validations like jQuery validation, I need each input field to have different name values.

View

<% @questions.each do |question| %>
    <%= form_for @lenderanswer, url: business_loan_question_answer_path(@user) do |f| %>
        <%= f.hidden_field :qanda_type, :value => "answer" %>
        <%= f.hidden_field :qanda_id, :value => question.Id %>
        <section>
            <label class="textarea">
                <%= f.text_area :answer, :placeholder => "Your answer.." %>
            </label>
        </section>
        <%= f.submit "Submit Answer", :class => "btn btn-u", :type => "submit" %>
    <% end %>
<% end %>

# HTML OUTPUT
<textarea id="loan_question_answer_answer" name="loan_question_answer[answer]" placeholder="Your answer.."></textarea>

I would like to somehow add "_1", "_2" and so on to the name value (depending on how many questions are iterated over. Is there a way for me to append a number value to the name value?

Upvotes: 1

Views: 65

Answers (1)

Lee Han Kyeol
Lee Han Kyeol

Reputation: 2481

You can use each_with_index to use index in each.

Upvotes: 1

Related Questions