2013-12-18 51 views
0

更新時,我得到這樣的錯誤。rails 3.2.13,update_attributes錯誤

Couldn't find PropertyAcceptanceCriterion with ID=14 for Property with ID=1 

當關閉複選框並更新(保存)時會發生此錯誤。

請教我這個原因,和我應該怎麼辦接下來,

模型定義是

class PropertyAcceptance < ActiveRecord::Base 
    belongs_to :property 
    belongs_to :property_acceptance_criterion 
end 

class PropertyAcceptanceCriterion < ActiveRecord::Base 
    attr_accessible :name 
    has_many :property_acceptances, dependent: :destroy 
    has_many :properties, through: :property_acceptances 
end 

class Property < ActiveRecord::Base 
    attr_accessible :rent 
    attr_accessible :room_no 
    attr_accessible :property_acceptance_criterions_attributes 
    attr_accessible :property_acceptance_criterion_ids 
    has_many :property_acceptances, dependent: :destroy 
    has_many :property_acceptance_criterions, through: :property_acceptances 
    accepts_nested_attributes_for :property_acceptance_criterions, reject_if: lambda { |a| a[:name].blank? } 
end 

視圖定義是

= simple_nested_form_for @property do |f| 
    = f.input :room_no, input_html: {class: 'span2'} 
    = f.input :rent, input_html: {class: 'span2'} 
    = f.association :property_acceptance_criterions, as: :check_boxes 
    = f.simple_fields_for :property_acceptance_criterions do |c| 
    = c.input :name, label: "add for #{t('activerecord.attributes.property.property_acceptance_criterions')}" if c.object.new_record? 

控制器的定義是

class Insurance::PropertiesController < Insurance::InsuranceController 
    def new 
    @property.property_acceptance_criterions.build 
    end 
    def create 
    @property.attributes = params[:property] 
    if @property.save 
     redirect_to @property, success: t('activerecord.flash.property.actions.create.success') 
    else 
     render :new 
    end 
    end 
    def edit 
    @property.property_acceptance_criterions.build 
    end 
    def update 
    if @property.update_attributes(params[:property]) # ← error occur 
     redirect_to @property, success: t('activerecord.flash.property.actions.update.success') 
    else 
     render :edit 
    end 
    end 
end 

錯誤是

Couldn't find PropertyAcceptanceCriterion with ID=14 for Property with ID=1 

params爲

{"utf8"=>"✓", 
    "_method"=>"put", 
    "authenticity_token"=>"+Zx7l7mAbX12PSO873x5NDxNOIeEe6bEDEdVnys+a98=", 
    "property"=>{ 
    "room_no"=>"000", 
    "rent"=>"80000", 
    "property_acceptance_criterion_ids"=>["13", "25", ""], 
    "property_acceptance_criterions_attributes"=>{ 
    "0"=>{"id"=>"13"}, "1"=>{"id"=>"14"}, "2"=>{"id"=>"25"}, "3"=>{"name"=>""} 
    }, 
    "commit"=>"update", 
    "id"=>"1"} 
+0

對不起,我忘記了這段代碼。 – den256

回答

0

嘗試這種在你的編輯

def edit 
    @property = Property.find(params[:id]) 
end 

請將此代碼添加到控制器

before_filter :load_property, only: [:edit, :update] 
    before_filter :new_property, only: [:new, :create] 

    ...... 

    private 

    def load_property 
    @property = Property.find(params[:id]) 
    end 

    def new_property 
    @property = Property.new 
    end 

錯誤發生,但,添加你的指示。

+0

這個工作嗎? @ user2977863 – joseramonc

+0

是的,創建工作。 – den256

+0

對不起,我是日本人,所以我不明白你的意思。 – den256

相關問題