我正在尝试使用具有belongs_to和has_one
关系的嵌套属性表单我在模型中定义了关系,如
class Merchant < ActiveRecord::Base
has_one :account, dependent: :destroy
class Account < ActiveRecord::Base
belongs_to :merchant
控制器
def edit
@merchant = Merchant.find(params[:id])
@merchant.account.build
@states = State.form_selector
@products = Product.all
@properties = Property.where(property_set_id: 1)
end
错误
NoMethodError (undefined method `build' for nil:NilClass):
app/controllers/admin/merchants_controller.rb:32:in `edit'
答案 0 :(得分:3)
has_one
关联的构建语法:
@merchant.build_account # this will work
@merchant.account.build # this will throw error
阅读has_one association documentation了解更多详情。
此外,您应该检查是否找到@marchant
id