2013-07-11 42 views
0

已經嘗試過這種常見錯誤的解決方案,但在這種情況下它們都不起作用,可能是因爲我之前已經在屬性上創建了一個地址字段。我不斷收到質量分配錯誤。無法批量分配受保護的屬性:

任何幫助表示讚賞。

Address.rb

class Address < ActiveRecord::Base 

    attr_accessible :addressable_id, :addressable_type, :city, :county, :postcode, :street1, :street2 

    belongs_to :addressable, :polymorphic => true 
end 

Property.rb

class Property < ActiveRecord::Base 
    attr_accessible :name, :addresses_attributes 
    belongs_to :user 

    has_one :address, :as => :addressable 
    accepts_nested_attributes_for :address 

    validates :name, presence: true, length: { maximum: 200 } 
    validates :address, presence: true 
    validates :user_id, presence: true 

end 

物業控制器

class PropertiesController < ApplicationController 
    before_filter :authenticate_user! 
    before_filter :correct_user, only: :destroy 


    def index 
    @property= Property.all 
    end 


    def create 
    @property = current_user.properties.build(params[:property]) 
    if @property.save 
     flash[:success] = " Property Added" 
     redirect_to root_path 
    else 
     render 'edit' 
    end 
    end 

    def new 
    @property = Property.new 
    @property.build_address 
    end 
+0

大衆分配錯誤?你能顯示錯誤信息嗎? –

+0

對不起,它是ActiveModel :: MassAssignmentSecurity :: Error at/properties 無法批量分配受保護的屬性:地址 – cyclopse87

回答

0

您應該添加

accepts_nested_attributes_for :address 

和更改

attr_accessible :name, :address_attributes 

,如果它不工作,什麼都改到addressaddressable

相關問題