我有一个模特: 班级简介 包括Mongoid :: Document
# PROFILE TYPE KIDS & PARENT
embeds_one :kids_type, :class_name => "ProfileKidsType"
embeds_one :parent_type, :class_name => "ProfileParentType"
end
并在ProfileKidsType模型中:
class ProfileKidsType
include Mongoid::Document
field :nickname, :type => String
field :gender, :type => String
....等等......
embedded_in :profile, :inverse_of => :kids_type
end
在 views:profiles /_form.html.haml
= form_for @profile do |f|
.formBox
.formSection Child information
= f.label :lname, "Nick name"
= f.text_field :nickname
我怎么能在这里访问昵称字段.....当我执行上面的代码时,它说的是未定义的方法。
答案 0 :(得分:0)
您需要使用fields_for为嵌入式文档创建嵌套表单。
这样的事情会起作用:
= form_for @profile do |f|
.formBox
.formSection Child information
= f.fields_for :kids_type do |k|
= k.label :nickname, "Nick name"
= k.text_field :nickname
有关更多详细信息,请参阅this post,但不使用haml。