无法批量分配受保护的属性:

时间:2013-07-11 23:42:53

标签: ruby-on-rails ruby-on-rails-3 mass-assignment

在stackoverflow中尝试过这个常见错误的解决方案,但在这种情况下它们似乎都没有用,可能是我之前在属性上创建了一个地址字段。我一直收到质量分配错误。

任何帮助表示感谢。

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

1 个答案:

答案 0 :(得分:0)

你应该添加

accepts_nested_attributes_for :address

并更改

attr_accessible  :name, :address_attributes

如果不起作用,请将address更改为addressable