0
我正在創建跨三種不同實體類型的標記系統。作爲一個關聯的多態屬性,我遇到了麻煩。編輯現有的聯繫人似乎可以工作(創建相關記錄),但創建新聯繫人時,它不會創建其他記錄。只有一個關聯記錄,因爲每個標記都以單個字符串形式記錄到一個長文本字段中。創建具有多態屬性的新實體
contact.rb:
class Contact < ActiveRecord::Base
has_one :tag, :as => taggable
accepts_nested_attributes_for :tag, :allow_destroy => true,
:reject_if => :all_blank
tag.rb:
class Tag < ActiveRecord::Base
belongs_to :taggable, :polymorphic => true
contacts_controller.rb:
def new
@contact = Contact.new
@contact.tag = Tag.new
end
def edit
@contact = Contact.find(params[:id])
if @contact.tag.nil?
@contact.tag = Tag.new
end
end
new.html.erb:
<%= fields_for :tag do |tag| %>
<div class="clearfix">
<%= tag.label :tags, 'Tags' %>
<div class="input">
<%= tag.text_field :tags %>
</div>
</div>
如果您執行'form.fields_for'而不是僅僅'bare_for_'(假設form是來自外部'form_for'的已生成表單的變量),會發生什麼? – AndrewF 2012-01-02 09:12:47
實際上,這會起作用。如果您將此評論移至答案,我會接受它。 – James 2012-01-03 04:48:47