Pass the integer value into ruby ​​on the rails

advertisements

I got a task to pass one integer value to another partial form. My code was this

<div class="fields">
  <p>
    <%= f.label :content, "Student" %><br/>
    <%= f.text_field :content %>
    <%= link_to_remove_fields "remove", f %><br />
  </p>
  <h4>Address</h4>
  <% i = 1 %>
  <% f.fields_for :answers do |builder| %>
    <%= render :partial => 'answer_fields', :locals => {:i => i, :f => builder} %>
    <% i = i + 1 %>
  <% end %>
  <p><%= link_to_add_fields "Add Address", f, :answers %></p>
</div>

But in the partial form i got an error like

undefined local variable or method `i'

My partial form is

<p class="fields">
  <table>
    <tr>
      <td>
        <%= f.label :content, "Address"+i.to_s %>
      </td>
      <td>
        <%= f.text_field :content %>
      </td>
      <td>
        <%= link_to_remove_fields "remove", f %>
      </td>
    </tr>
  </table>
</p>

How can i pass the value of i to other partial form?


your code is correct.

   Please try with this..

   <%= render :partial => 'answer_fields', :locals => {:i => i.to_i, :f => builder} %>