【问题标题】:Mongo db Array with rails 3带有rails 3的Mongodb数组
【发布时间】:2012-06-28 05:05:11
【问题描述】:

我有一个模型: 班级简介 包括 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

在 意见:个人资料/_form.html.haml

    = form_for @profile do |f|

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

我如何在这里访问昵称字段.....当我执行上面的代码时,它说的是未定义的方法。

【问题讨论】:

    标签: ruby-on-rails mongodb


    【解决方案1】:

    您需要使用 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。

    【讨论】:

    • 您将使用相同的代码,只需像上面那样将其放在部分中:profiles /_form.html.haml,然后从另一个(非部分)视图(如profiles/edit)呈现它。 html.haml 或profiles/new.html.haml
    猜你喜欢
    • 1970-01-01
    • 2014-11-04
    • 1970-01-01
    • 2019-12-11
    • 1970-01-01
    • 1970-01-01
    • 2011-09-16
    • 2013-12-12
    • 1970-01-01
    相关资源
    最近更新 更多