开发者

Rails 3: nested form rendered with belongs_to association

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.

Model:

Store.rb

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

end

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

Controller

stores_controller.rb

  def new
    @store = Store.new
    @store.build_store_address
    respond_with(@store)
  end

View

new.html.erb

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

  </p>
<% end %>

_form.html.erb

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

<fieldset> 
    <legend><label><%=t(:address)%></label></legend> 
    <% f.fields_for :store_address do |address_form| %>
    <table> 
        <tbody><tr> 
            <td width="200"&g开发者_Go百科t;<label><%=t(:line_1)%></label></td><td> 
            <%= address_form.text_area :address1, :rows => 2%> 
            </td> 
        </tr> 
        <tr> 
            <td><label><%=t(:line_2)%></label></td><td> 
            <%= address_form.text_area :address2, :rows => 2 %>  
            </td> 
        </tr> 
        <tr> 
            <td><label><%=t(:city)%></label></td><td> 

            <%= address_form.text_field :city %> 

            </td> 
        </tr> 
.......

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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜