1

您如何接受使用继承的模型的嵌套属性?

Rails 3.2,Mongoid 3.1

class User
    include Mongoid::Document
    include Mongoid::Timestamps

    field :name, type: String

    attr_accessible :name

    embeds_many :cards
    attr_accessible :cards_attributes
    accepts_nested_attributes_for :cards

end

用户类嵌入了许多类型的卡片。

class Card
    include Mongoid::Document
    include Mongoid::Timestamps

    embedded_in :user
end

class HealthCard < Card
    field :number,      type: String

    attr_accessible :expiry_date, :number
end

class StudentCard < Card
    field :expiry_date, type: Date
    field :number,      type: String
    field :dept,        type: String


    attr_accessible :expiry_date, :number, :dept
end

使用下面的表格

= form_for @user do |user_form|

    = user_form.fields_for :health_cards, @health_card do |hc_form|
        = hc_form.text_field :number

    = user_form.submit 'Next'

在提交时,我观察以下参数

{"user"=>{"health_cards"=>{"number"=>"6564 - 082 - 649 - AM"} }, "utf8"=>"✓", "_method"=>"put", "authenticity_token"=>"C0DIKNbccfDQ=", "commit"=>"Next", "action"=>"update", "controller"=>"person_steps"}
4

1 回答 1

1

问题出在形式上,应该user_form.fields_for :cards:health_cards

下面是修正版

= form_for @user do |user_form|

    = user_form.fields_for :cards, @health_card do |hc_form|
        = hc_form.text_field :number

    = user_form.submit 'Next'
于 2013-06-17T04:02:30.600 回答