Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7058679
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:08:29+00:00 2026-05-28T04:08:29+00:00

I have 10 form fields and I want to append a number next to

  • 0

I have 10 form fields and I want to append a number next to each field, going sequentially from 1 to 10. The problem is that the code is already in a loop. It’s inside a partial, and the partial is passed a collection.

<%= fields_for "list", f do |f| %>
    <!-- I want 1, 2, etc to appear here depending on the iteration. -->
    <%= f.label :name %>

I tried using <%= i += 1 %> but it does not work since i is not defined. If I define i, it will keep getting reset to the same number, so it makes no difference. Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T04:08:30+00:00Added an answer on May 28, 2026 at 4:08 am

    Rails automatically defines a local variable called partialname_counter where, obviously, “partialname” is the name of your partial. So if your partial is called e.g. _list_item.html.erb you could write it like this:

    <%= fields_for "list", f do |f| %>
      <%= list_item_counter + 1 %>
      <%= f.label :name %>
    <% end %>
    

    (The + 1, of course, is there because the counter starts at 0.)

    Another option would be to just let the browser do the numbering for you using an ordered list:

    In the view:

    <ol>
      <%= render :list_item, :collection => @some_items %>
    </ol>
    

    …and in the partial:

    <%= fields_for "list", f do |f| %>
      <li>
        <%= f.label :name %>
      </li>
    <% end %>
    

    This option is probably more semantic, and lists are easy to style in CSS.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a form that i want to auto populate the last 2 fields
I have a basic PHP form (a few fields and 2 checkboxes, I want
I want to make a user-registration form where I will have fields such first
I have an input field in a form. Upon pushing submit, I want to
I have written this code where I want to increase / decrease number of
I have some models and I want to generate a multi-selection form from this
I have a script that writes a value from a form to a <td>
I have a form that I copied over from new.html.erb and put it in
I don't want to have a formal form field but save every user input
I have an html form where on the input field selection from a drop

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.