0

Show モデルの Form_for があります。form_for 内で fields_for を使用してバンドを追加したいと思います。問題は、フォームを使用してレコードを更新するときに、フィールドをバンドに関連付けたくないということです。バンド名が変わったら、新しいバンドでパフォーマンスを更新したいと思います。

ショーはパフォーマンスを通じてバンドと結合されます

class Show < ActiveRecord::Base
  has_many :performances
  has_many :bands, through: :performances
  accepts_nested_attributes_for :bands
end

class Band < ActiveRecord::Base
  attr_accessible :name, :website, :country, :state
  has_many :performances
  has_many :shows, through: :performances

  validates :name, presence: true, uniqueness: true
end

class Performance < ActiveRecord::Base
  attr_accessible :show, :band
  belongs_to :show
  belongs_to :band
end

これが私のフォームです。(簡体字)

<%= form_for @show do |f| %>
     #fields
    <%= f.fields_for :bands do |b| %>
      <%= b.text_field :name %>      
    <% end %>
<%end>

問題は、これを使用してバンド名を変更すると、バンド名が変更されることです (クレイジーですよね?)。Band レコードを更新したくありません。Band.find_or_create を実行し、パフォーマンス レコードを新しいバンドの ID で更新したいのです。このようにして、ユーザーは名前を削除して別のバンド名を追加することで、ショーのバンドを置き換えることができます。

レンダリングされたhtmlには、バンドIDではなくパフォーマンスIDが含まれている必要があります(私は思う)

何かのようなもの:

<input id="show_performance_attributes_1_id" name="show[performance_attributes][1][id]" type="hidden" value="62">

これはどのように行われますか?

4

1 に答える 1