0

https://github.com/ryanb/nested_formで説明されているように、nested_form gem を設定しています。

問題は、私の f.fields_for が空に戻ったように見えることです。エラーはありませんが、ページには何もしない「ロケーションの追加」リンクしか表示されません。

データ モデルは Events => event_locations <= Locations であり、event_locations はジャンクション オブジェクトとして機能します。

これが私の見解のフォームです:

 <%= nested_form_for @event do |f| %>
    <%= @event.locations %>
    <%= f.fields_for :locations do |task_form| %>
      <%= task_form.text_field :name %>
      <%= task_form.link_to_remove "Remove this Location" %>
    <% end %>

    <p><%= f.link_to_add "Add a Location", :locations %></p>
  <% end %>

ここに私のイベントコントローラがあります:

def new
    logger.debug "*** before Event.new"
    @event = Event.new
    logger.debug "*** after Event.new"
    3.times do
      @event.locations.build
    end   
  end

event.rb

has_many :days_events
  has_many :events_locations
  has_many :locations, :through => :events_locations

  # Validations
  validates_presence_of :name

  accepts_nested_attributes_for :locations

event_location.rb

belongs_to :event
belongs_to :location

accepts_nested_attributes_for :location

location.rb

has_many :events_locations
has_many :events, :through => :events_locations


#validations
validates_presence_of :name

accepts_nested_attributes_for :events
4

1 に答える 1

0

nested_forms gem は少し古いです。スクリーン キャストの改訂されたエピソード ( http://railscasts.com/episodes/196-nested-model-form-revised )で ryan のコードを使用することをお勧めします。見るにはプロアカウントが必要だと思いますが、それだけの価値があります。

または、元のスクリーン キャスト ( http://railscasts.com/episodes/197-nested-model-form-part-2 ) を修正せずに無料で使用し、そこからコードを使用します。そうすれば、少なくとも何が起こっているかがわかります。

于 2013-02-07T19:19:33.743 に答える