I have two models: Album and Track. Album has many tracks, and Track belongs to album.
I'd like to have the ability to create as many tracks as needed while creating the album, similailiy to railscasts episode 197. Unlike the railscasts episode, though, the Track form contains both a title and a description - both are required.
Right now, the form looks like this:
Create New Album
Name: [ ]
Track (remove link)
Name: [ ]
Description: [ ]
Track (remove link)
Name: [ ]
Description: [ ]
(add track link)
If I decide to submit the form blank, I get the following error messages on top of the form:
Description can't be blank
Title can't be blank
Title can't be blank
These error messages are not specific to the model, all located at the top of the page, and appear only once for each model (note that I left the fields for both blank and the error messages appear only once - not specific to which track).
To generate the initial track fields, I added the following line in the new action of my album_controller: 2.times { @album.tracks.build }
The gist of what my form looks like is this:
<% form_for @album do |f| %>
<%= f.error_messages %>
<%= f.label :title %><br />
<%= f.text_field :title %>
<% f.fields_for :tracks do |f, track| %>
<%= render :partial => 'tracks/fields', :locals => {:f => f} %>
<% end %>
<%= f.submit "Submit" %>
<% end %>
I tried replacing the top <%= f.error_messages %>
with <%= error_messages_for @album %>
(to only display the messages for the album), and adding a <%= error_messages_for track %>
(to display the error messages specific to each track) - but this does not do the trick.
Does anybody know how to approach this?
Thanks!