Take the 2-minute tour ×

I am struggling to get the nested form rendered with belongs_to association.

I was expecting the address fields in the "_form.html.erb" (below) to be rendered correct, but apparently it is not, and i just cant wrap my head around it to figure why!

Rails version: 3.09

here is the code.



class Store < ActiveRecord::Base
  has_and_belongs_to_many :products
  belongs_to :store_address, :foreign_key => "address_id", :class_name => "Address"


Address doesn't have any reference to Store model (it is independent)



  def new
    @store = Store.new



<% form_for(@store, :url => collection_url) do |f| %>
  <p class="form-buttons">
    <%= button t('continue') %>

<% end %>


<%=t(:store_name)%> : <%= text_field :store, :name %>
<%=t(:store_admin_email)%> : <%= text_field :store, :admin_email %>

    <% f.fields_for :store_address do |address_form| %>
            <td width="200"><label><%=t(:line_1)%></label></td><td> 
            <%= address_form.text_area :address1, :rows => 2%> 
            <%= address_form.text_area :address2, :rows => 2 %>  

            <%= address_form.text_field :city %> 


This renders the store name. but nothing on the address side. please help!

share|improve this question
Did you ever get this working to completion? You have a similar setup to me, however, when I try and submit the form it doesn't create the store_address. –  Altonymous Oct 11 '11 at 18:19
add comment

1 Answer

up vote 3 down vote accepted

I think you missed an = at <% f.fields_for .... It should be <%= f.fields_for... .

This has happened to me so often in the past, missing this one thing, and then wondering why the nested form would not render.

share|improve this answer
awww... i knew it had to be something silly.. didn't think it was this silly though ;) –  thanikkal Aug 9 '11 at 15:14
its plain silly, but very easy to get wrong. i abandoned my program for days when I went through the same situation. :) –  rookieRailer Aug 9 '11 at 15:25
add comment

Your Answer


