2011-05-11 148 views
3

我在我的應用程序中定義了以下型號Rails 3箇中嵌套表格 - 多級

  • 用戶
  • 簡介
  • 地址

這裏是我的用戶模型:

class User < ActiveRecord::Base 

    # Include default devise modules. Others available are: 
    # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable 
    devise :database_authenticatable, :registerable, 
     :recoverable, :rememberable, :trackable, :validatable 

    # Setup accessible (or protected) attributes for your model 
    attr_accessible :email, :password, :password_confirmation, :remember_me, :profile_attributes 

    has_one :profile 
    accepts_nested_attributes_for :profile 
end 

這是我的個人資料模型:

class Profile < ActiveRecord::Base 

    attr_accessible :first_name, :last_name, :organization, :telephone_number, :user_id, :address_attributes 

    belongs_to :user 
    has_one :address 

    accepts_nested_attributes_for :address 
end 

這是我的地址模型:

class Address < ActiveRecord::Base 
    attr_accessible :street, :street_cont, :city, :state, :zip_code 
    belongs_to :profile 
end 

我使用的設計進行驗證所以我認爲我已在登記以下內容:

<% resource.build_profile %> 
<h2>Sign up</h2> 

<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %> 
    <%= devise_error_messages! %> 

    <p><%= f.label :email %><br /> 
    <%= f.email_field :email %></p> 

    <p><%= f.label :password %><br /> 
    <%= f.password_field :password %></p> 

    <p><%= f.label :password_confirmation %><br /> 
    <%= f.password_field :password_confirmation %></p> 

    <%=f.fields_for :profile do |profile_form| %> 
     <p><%= profile_form.label :first_name %><br /> 
     <%= profile_form.text_field :first_name %></p> 

     <p><%= profile_form.label :last_name %><br /> 
     <%= profile_form.text_field :last_name %></p> 

     <p><%= profile_form.label :organization %><br /> 
     <%= profile_form.text_field :organization %></p> 

     <p><%= profile_form.label :telephone_number %><br /> 
     <%= profile_form.text_field :telephone_number %></p> 

     <%=f.fields_for :address do |address_form| %> 
     <p><%= address_form.label :street %><br /> 
     <%= address_form.text_field :street %></p> 

     <p><%= address_form.label :street_cont %><br /> 
     <%= address_form.text_field :street_cont %></p> 

     <p><%= address_form.label :city %><br /> 
     <%= address_form.text_field :city %></p> 

     <p><%= address_form.label :state %><br /> 
     <%= address_form.text_field :state %></p> 

     <p><%= address_form.label :zip_code %><br /> 
     <%= address_form.text_field :zip_code %></p> 
    <% end %> 
    <% end %> 

    <p><%= f.submit "Sign up" %></p> 
<% end %> 

<%= render :partial => "devise/shared/links" %> 

的形式呈現正確的,但是當我查看我看到的地址字段的來源:

<input id="user_address_street" name="user[address][street]" size="30" type="text" /> 

的型材斷面我看到:

<input id="user_profile_attributes_first_name" name="user[profile_attributes][first_name]" size="30" type="text" /> 

當我保存表單,用戶和配置文件保存到數據庫中,但沒有地址。我很明顯可能在模型關係上做錯了事,但我不知道如何去解決這個問題。

任何幫助,將不勝感激。

+0

你爲什麼要使用嵌套的HTML表單?如果不是功能上不正確的話,我認爲這在邏輯上是不正確的。看到http://stackoverflow.com/questions/379610/can-you-nest-html-forms – Gal 2011-05-11 19:27:41

回答

3

更改此:

<%=f.fields_for :address do |address_form| %> 

要這樣:

<%=profile_form.fields_for :address do |address_form| %> 
+0

我只是試過,雖然我沒有得到一個錯誤,地址字段不顯示在窗體上。 – Mike 2011-05-11 19:35:43

+0

您可能必須先在Person上創建一個空的地址對象。 – twmills 2011-05-11 19:47:22

+0

@ user.person.address = Address.new – twmills 2011-05-11 19:48:43