views:

32

answers:

3

Hi, in the following code, I want to assign a different label to the text field for each iteration.

<%= f.field_for :skills do |s|  %>
    <li>
      <label>Skills</label>
      <%= s.text_field :name %>
    </li>
<% end %>

How can I do that? Here is my controller code, where I create three different skill objects:

def edit
    3.times{resource.skills.build}
    render_with_scope :edit
end
A: 

Just scope the label to s:

<%= f.field_for :skills do |s|  %>
<li>
  <%= s.label :name, 'Skills' %>
  <%= s.text_field :name %>
</li>
<% end %>

I think that should do it.

nfm
A: 

by assuming s is an object and contained value of (s.skills)

   <%= f.field_for :skills do |s|  %> 
        <li> 
          <label> <%= s.text_field :Skills %> </label> 
          <%= s.text_field :name %> 
        </li> 
    <% end %>

I hope your issue resolve by this.

SaifalMaluk
Do you actually know ruby-on-rails? What you write is so terribly wrong.
nathanvda
+1  A: 

You could do something like this:

<% counter = 0 %>
<%= f.fields_for :skills do |s|  %>
  <li>
    <%= s.label :name, "Skill #{counter}" %>
    <%= s.text_field :name %>
    <% counter = counter + 1 %>
  </li>
<% end %>

It is preferred to use s.label :name, since that will make sure when you click the label, the text-box will get the focus. But the value of the label can be overruled, as i did here.

I am not quite sure what else you could mean with changing the label for each item, so if you could make that clearer.

Hope this helps.

nathanvda