2012-06-28 51 views
0

我有一个模型: 类资料 包括Mongoid ::文献蒙戈分贝与导轨阵列3

# 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 

in views:profiles /_form.html.ham l

= form_for @profile do |f| 

    .formBox 
    .formSection Child information 
    = f.label :lname, "Nick name" 
    = f.text_field :nickname 

我怎样才能访问昵称字段在这里.....当我执行上面的代码它说的未定义的方法。

回答

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。

+0

如果我想要在部分页面中,那么.. – Agnes

+0

您可以使用相同的代码,只需像上面那样部分地将其放入:profiles /_form.html.haml,然后从另一个(非 - 部分)视图,如profiles/edit.html.haml或profiles/new.html.haml –