Search code examples
ruby-on-railsruby-on-rails-3formsruby-on-rails-3.1fields-for

rails 3.1 fields_for


Here is my fields_for block in my edit view of the orders controller

  <%= form_for @order do |f| %>

    <%= f.fields_for :order_fields do |builder| %>
      <%= builder.text_field :value %>
    <% end %>

  <% end %>

The problem I am having is that I would like to use an each loop to display information from the loop, but then i get the form displayed more than once. this is because i used the accepts_nested_attributes_for :order_fields statement.

Inside my order_fields table there is a column named name that I would like to display the information from in the form. How can I do this? The information from the value column in my database is displayed in my form already.

If i do

<%= f.fields_for :order_fields do |builder| %>
  <%= builder.text_field :name %>
  <%= builder.text_field :value %>
<% end %>

it displays information from both columns next to each other, but I would like the :name information not to be in a text box or anything. I would like it to be displayed as if i were to do something like:

<% @order.order_fields.each do |field| %>
  <%= field.name %>
<% end %>

I hope this make some sense :) Thank you.


Solution

  • You can access the object off of |builder| by calling .object on it

    <%= f.fields_for :order_fields do |builder| %>
      <%= builder.object.name %>
      <%= builder.text_field :value %>
    <% end %>