基本多态轨道形式

时间:2011-06-14 16:05:11

标签: ruby-on-rails forms polymorphic-associations

查看(此处需要帮助)或者我是否需要更改accepts_nested_attributes。我需要一个创建属性和相关地址的表单,从City开始。此时,我收到以下错误:unknown attribute: address。此外,我想在创建后重定向;不知道如何使用inherited_resources做到这一点。谢谢你。

= form_for Property.new do |f|
  = f.fields_for :address do |builder|
    = builder.label :city
    = builder.text_field :city
  = f.submit

class Property < ActiveRecord::Base
  include ActiveRecord::Transitions
  include ActsAsAsset
  include Amendable
  include Searchable

  acts_as_taggable_on :tags

  has_many :addresses, :as => :addressable
  has_many :escrows
  has_many :events, :as => :entity
  has_many :phone_numbers, :as => :phoneable

  accepts_nested_attributes_for :addresses
  .
  .
  .
end
class Address < ActiveRecord::Base
  belongs_to :addressable, :polymorphic => true
  belongs_to :address_category
  has_many :notes, :as => :notable

  validate :not_blank
  validates :addressable, :presence => true

  private

    def not_blank
      self.errors.add :base, 'cannot be blank' if
        self.attributes.values.all? {|attr| attr.blank? }
    end
end

class PropertiesController < ApplicationController
  inherit_resources

  before_filter :authenticate_user!

  def index
  end

  def show
  end
end

1 个答案:

答案 0 :(得分:2)

由于它是has_many关系,因此您需要'地址'而不是'地址'。

应该是这样的:

= form_for Property.new do |f|
  = f.fields_for :addresses do |builder|
    = builder.label :city
    = builder.text_field :city
  = f.submit