Mongoid- accepted_nested_attributes_for不保存

时间:2012-05-29 19:29:04

标签: ruby-on-rails mongoid has-many

我在EnquiryFormUniversityFeeInstallment EnquiryForm has_many UniversityFeeInstallment之间有一对多的关系。
以下是我从控制器

收到的params
{
"utf8"=>"✓",
"authenticity_token"=>"jqgiRlk606pDzMEAtS/mGoWz8T61PgyCkKdMzSHEiQA=",
"enquiry"=>{
    "university_fee_installments_attributes"=>{
        "1338318502006"=>{
            "due_date"=>"2012-05-28",
            "amount"=>"1200"
        }
    }
},
"commit"=>"Update Enquiry",
"id"=>"4fc3db492d6d130238000028"

}

我正在使用Ryan Bates经典的嵌套表单技术。 模型代码也是:

 has_many :development_fee_installments, :autosave => true 
  has_many :university_fee_installments, :autosave => true 
  accepts_nested_attributes_for :development_fee_installments
  accepts_nested_attributes_for :university_fee_installments

控制器:

def update
  @enquiry = Enauiry.find(params[:id])
  if @enquiry.save
    redirect_to enquiry_payments_path(@enquiry, :notice => "Installment details updated")
  else
    render 'edit_installments'
  end
end

我无法保存university_fee_installments。

1 个答案:

答案 0 :(得分:1)

将您的控制器代码更改为此

def update
    @enquiry = Enquiry.find(params[:id])
    if @enquiry.update_attributes(params[:enquiry])
        redirect_to enquiry_payments_path(@enquiry, :notice => "Installment details updated")
    else
        render 'edit_installments'
    end
end

update_attributes将完成这一操作,因为我们传递了从视图中收到的参数。