微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

基本多态导轨形式

如何解决基本多态导轨形式

| 视图(在此需要帮助)还是我需要更改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
    

解决方法

        由于这是一个2关系,因此您需要\'addresses \'而不是\'address \'。 应该是这样的:
= form_for Property.new do |f|
  = f.fields_for :addresses do |builder|
    = builder.label :city
    = builder.text_field :city
  = f.submit
    

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。