0
私は3つの異なるエンティティタイプにわたってタグ付けシステムを作成しています。私はそれを関連する多型属性として実装するのに苦労しています。既存の連絡先を編集すると(関連付けられたレコードが作成されます)、新しい連絡先を作成するときには別のレコードが作成されません。各タグは単一の文字列として1つの長いテキストフィールドに記録されるため、関連付けられたレコードは1つだけです。多態性属性を持つ新しいエンティティを作成する
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>
裸の 'fields_for'の代わりに' form.fields_for'を実行するとどうなりますか(フォームが外側のform_forから得られたフォームの変数であると仮定します) – AndrewF
実際にはうまくいきました。このコメントを回答に移すと、私はそれを受け入れます。 – James