gpt4 book ai didi

ruby-on-rails - Rails 3 中的嵌套属性

转载 作者:行者123 更新时间:2023-12-03 16:07:16 25 4
gpt4 key购买 nike

任何人都可以带我了解 Rails 3 中的嵌套属性吗?

我有两个模型:证书和托管人,相关如下:

证书型号:

class Certificate < ActiveRecord::Base
belongs_to :shareholder
belongs_to :custodian
belongs_to :issuer

accepts_nested_attributes_for :custodian, :shareholder, :issuer
end

证书 Controller :
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 -%>

现在,出于某种原因,在我的 Controller 第 4 行: @certificate.custodian.build

我收到此错误: undefined method 'build' for nil:NilClass
任何人都可以帮忙吗?

最佳答案

使用belongs_to,它应该是

@certificate.build_custodian

关于ruby-on-rails - Rails 3 中的嵌套属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4867880/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com