已尝试解决 stackoverflow 中此常见错误的解决方案,但在这种情况下似乎都不起作用,可能是我之前已经在该属性上创建了一个地址字段。我不断收到质量分配错误。
任何帮助表示赞赏。
地址.rb
class Address < ActiveRecord::Base
attr_accessible :addressable_id, :addressable_type, :city, :county, :postcode, :street1, :street2
belongs_to :addressable, :polymorphic => true
end
属性.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