I donβt see a decent way to do this using the methods provided by Rails, at least not in -v3.2.14
@Sheharyar Naseer refers to hash parameters that can be used to solve the problem, but not as much as I can see in how it seems to suggest.
I did it =>
<%= f.fields_for :blog_posts, {:index => 0} do |g| %> <%= g.label :gallery_sets_id, "Position #{g.options[:index]}" %> <%= g.select :gallery_sets_id, @posts.collect { |p| [p.title, p.id] } %> <%
or
<%= f.fields_for :blog_posts do |g| %> <%= g.label :gallery_sets_id, "Position #{g.object_name.match(/(\d+)]/)[1]}" %> <%= g.select :gallery_sets_id, @posts.collect { |p| [p.title, p.id] } %> <% end %>
In my case, g.object_name returns a string similar to this "gallery_set[blog_posts_attributes][2]" for the third rendered field, so I just map the index on that line and use it.
In fact, a cooler (and perhaps a cleaner one?) The way to do this is to pass the lambda and call it to increase.
And in the view
<%= f.fields_for :blog_posts do |g| %> <%= g.label :gallery_sets_id, "Position #{@incrementer.call}" %> <%= g.select :gallery_sets_id, @posts.collect { |p| [p.title, p.id] } %> <% end %>
iNulty Oct 02 '13 at 16:10 2013-10-02 16:10
source share