listenlight
listenlight

Reputation: 662

Basic Polymorphic Rails Form

view (need help here) or do I need to change accepts_nested_attributes. I need a form that creates a property and associated address, beginning with City. At this point, I get the following error: unknown attribute: address. Further, I want to redirect back after create; not sure how to do that using inherited_resources. Thank you kindly.

= 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

Upvotes: 0

Views: 583

Answers (1)

Sam 山
Sam 山

Reputation: 42863

Since it is a has_many relationship you need 'addresses' not 'address'.

Should be this:

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

Upvotes: 2

Related Questions