任何人都可以請我通過Rails 3中的嵌套屬性?Rails 3中的嵌套屬性
我有兩個型號:證書和託管人,相關如下:
證書型號:
class Certificate < ActiveRecord::Base
belongs_to :shareholder
belongs_to :custodian
belongs_to :issuer
accepts_nested_attributes_for :custodian, :shareholder, :issuer
end
證書控制器:
class CertificateController < ApplicationController
def issue
@certificate = Certificate.new
@certificate.custodian.build
end
end
筆者認爲:
<% form_for(:certificate, :url => {:action => 'testing'}) do |f| -%>
<div id="error">
<%= f.error_messages %>
</div>
<%= f.label :number, "Certificate Number" %>
<%= f.text_field :number %> <br/>
<%= f.label :num_of_shares, "Number Of Shares" %>
<%= f.text_field :num_of_shares %> <br/>
<% f.fields_for :custodian do |custodian| -%>
<%= custodian.label :name, "Custodian Name" %>
<%= custodian.text_field :name %>
<% end -%>
<%= f.submit "Issue Certificate", :disable_with => 'Working....' %>
<% end -%>
現在,出於某種原因,在我的控制器上線4:@certificate.custodian.build
我得到這個錯誤:undefined method 'build' for nil:NilClass
任何一個可以請幫助?
感謝`Pejic`多數民衆贊成正是我想要實現的。但我不打算選擇框,我打算使用Ajax創建自動填充字段。但現在,我有興趣按照您所描述的方式完成這項工作。 – 2011-02-01 21:38:54