1

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

楷模

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={}> 
4

1 回答 1

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 

:camper 到 :bill。

但是现在我遇到了一个批量分配错误,我正试图解决这个错误。如果有人可以提供帮助: 使用带有多个模型的 Wicked Wizard 的批量分配错误

于 2013-05-27T15:19:26.867 回答