views:

25

answers:

1

I have a question relating to minimizing some code I'm working with. I am using the map-fields plugin/gem to allow my user to select what fields in a CSV file will map to attributes in a model.

map-fields with upload

The map-fields plugin uses the name of a select object in the plugin to determine where the CSV columns match up too. Here is my current code to get this all integrated.

  <% i = 1 %>
    <% @rows[0].each do |row| -%>
      <tr>
        <td><%= select_tag "fields[#{i}]", options_for_select(@fields), :include_blank => true, :class => 'field_options' %></td>
        <td><%= h(row) %></td>
      </tr>
      <% i += 1 %>
    <% end -%>

I want to clean this up a bit and instead of using 'i' I'd like to use some fancy ruby code to make it look clean.

Any thoughts?

+1  A: 

What exactly do you mean by fancy? you can remove the i by using each_with_index ( http://ruby-doc.org/core/classes/Enumerable.html#M003137 )

 <% @rows[0].each_with_index do |row,i| -%>
  <tr>
    <td><%= select_tag "fields[#{i}]", options_for_select(@fields), :include_blank => true, :class => 'field_options' %></td>
    <td><%= h(row) %></td>
  </tr>
<% end -%>

But other than that, I not sure what sorta fancy code you are looking for...

Doon
fancy meaning I wanted to be able to handle this without declaring the variable 'i'.This is the tip I needed.Thanks
Ryan