【问题标题】:update_attributes won't work for nested model in Wicked wizard rails 3.2update_attributes 不适用于 Wicked Wizard rails 3.2 中的嵌套模型
【发布时间】:2013-05-25 23:09:32
【问题描述】:

我有两个模型,Bill 和 Camper。比尔有很多露营者,但当我调用更新属性时它不会保存。我没有收到任何错误,我的数据库只是没有更新。我所有的露营者值都是零,但露营者对象始终是通过与账单的正确关联创建的。有人有什么想法吗?我正在使用 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={}> 

【问题讨论】:

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


    【解决方案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 to :bill.

    但现在我遇到了一个批量分配错误,我正试图解决它。如果有人可以提供帮助: Mass Assignment Error using Wicked Wizard with Multiple Models

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多