update_attributes不适用于Wicked向导rails 3.2中的嵌套模型

时间:2013-05-25 23:09:32

标签: ruby-on-rails database model ruby-on-rails-3.2 nested-attributes

我有两个模特,比尔和露营。比尔有很多露营者但是当我调用更新属性时它不会保存。我没有收到任何错误,我的数据库只是没有更新。我的所有露营者价值都是零,但露营者对象始终与账单正确关联。有人有什么想法吗?我正在使用邪恶的宝石生成一个向导。以下是我的代码:

模型

class Bill < ActiveRecord::Base
  has_many :campers
  accepts_nested_attributes_for :campers
  attr_accessible :email, :addressone, :addresstwo, :cellnum, :city, :firstname, :heard, :homenum, :lastname, :referred, :state, :worknum, :zip, :status, :comments, :campers, :campers_attributes
end

class Camper < ActiveRecord::Base
    belongs_to :bill
    has_many :camps
    attr_accessible :addressone, :addresstwo, :age, :city, :comments, :doctor, :emergencycontact, :firstname, :guardian, :health, :lastname, :medical, :state, :zip
end

控制器

class BillStepsController < ApplicationController
  include Wicked::Wizard
  steps :parent_registration, :camper_registration

  def show
    @bill = current_bill
    render_wizard
  end

  def update
    @bill = current_bill
    @camper = current_bill.campers.new
    case step
    when :parent_registration
            @bill.update_attributes(params[:bill])
            render_wizard @bill
    when :camper_registration
            @camper.update_attributes(params[:camper])
            render_wizard @camper
    end   
  end    
end

我在rails控制台中执行了以下操作:

1.9.3p125 :012 > c = Camper.new                                            
 => #<Camper id: nil, firstname: nil, lastname: nil, addressone: nil, addresstwo: nil, city: nil, state: nil, zip: nil, age: nil, emergencycontact: nil, health: nil, medical: nil, doctor: nil, guardian: nil, comments: nil, bill_id: nil, created_at: nil, updated_at: nil>                                                              
1.9.3p125 :013 > c.save                                            
   (0.3ms)  BEGIN                                                                  
  SQL (1.0ms)  INSERT INTO "campers" ("addressone", "addresstwo", "age", "bill_id", "city", "comments", "created_at", "doctor", "emergencycontact", "firstname", "guardian", "health", "lastname", "medical", "state", "updated_at", "zip") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17) RETURNING "id"  [["addressone", nil], ["addresstwo", nil], ["age", nil], ["bill_id", nil], ["city", nil], ["comments", nil], ["created_at", Sun, 26 May 2013 15:13:26 UTC +00:00], ["doctor", nil], ["emergencycontact", nil], ["firstname", nil], ["guardian", nil], ["health", nil], ["lastname", nil], ["medical", nil], ["state", nil], ["updated_at", Sun, 26 May 2013 15:13:26 UTC +00:00], ["zip", nil]]
   (23.0ms)  COMMIT
 => true 
1.9.3p125 :014 > pp c.errors
#<ActiveModel::Errors:0x0000000307f1e0
 @base=
  #<Camper id: 26, firstname: nil, lastname: nil, addressone: nil, addresstwo: nil, city: nil, state: nil, zip: nil, age: nil, emergencycontact: nil, health: nil, medical: nil, doctor: nil, guardian: nil, comments: nil, bill_id: nil, created_at: "2013-05-26 15:13:26", updated_at: "2013-05-26 15:13:26">,
 @messages={}>
 => #<ActiveModel::Errors:0x0000000307f1e0 @base=#<Camper id: 26, firstname: nil, lastname: nil, addressone: nil, addresstwo: nil, city: nil, state: nil, zip: nil, age: nil, emergencycontact: nil, health: nil, medical: nil, doctor: nil, guardian: nil, comments: nil, bill_id: nil, created_at: "2013-05-26 15:13:26", updated_at: "2013-05-26 15:13:26">, @messages={}> 

1 个答案:

答案 0 :(得分:1)

我弄清楚出了什么问题,我改变了:

case step
when :parent_registration
        @bill.update_attributes(params[:bill])
        render_wizard @bill
when :camper_registration
        @camper.update_attributes(params[:camper])
        render_wizard @camper
end 

为:

case step
when :parent_registration
        @bill.update_attributes(params[:bill])
        render_wizard @bill
when :camper_registration
        @camper.update_attributes(params[:bill])
        render_wizard @camper
end 

:露营者:账单。

但是现在我有一个质量分配错误,我正试图解决。如果有人可以帮忙: Mass Assignment Error using Wicked Wizard with Multiple Models