show model

1
2
3
4
5
class Show < ActiveRecord::Base
  has_many :pictures

  accepts_nested_attributes_for :pictures, :allow_destroy => true
end

picture model

1
2
3
class Picture < ActiveRecord::Base
  belongs_to :show
end

show controller

1
2
3
4
5
6
7
8
9
def new
    @show = Show.new
    @show.pictures.build

    respond_to do |format|
      format.html # new.html.erb
      format.json { render :json => @show }
    end
end

form

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<%= form_for(@show) do |f| %>
  <div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </div>

  <% f.fields_for :pictures do |picture_form| %>
    <div class="field">
      <%= picture_form.label :name %>
      <br />
      <%= picture_form.text_field :name %>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :notes %><br />
    <%= f.text_area :notes %>
  </div>

  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>