Models with Associations 0
Since each song belong to an artist (or multiple artists, depending on how complicated you want to make things), when adding a new song to the database, you want to be able to select the artist from a drop-down list.
In my admin controller, I’ve set up the following:
admin_controller.rb
def add_song
@artists = Artist.find_all
end
This gets me a list of all the artists in the database and stores them in @artists.
Now, in my template then, I can add the following code (adapted from some Recipe example I came across online):
add_song.rhtml
<select name="new_song[artist_id]">
<% @artists.each do |artist| %>
<option value="<%= artist.id %>">
<%= artist.first_name %> <%= artist.last_name %>
</option>
<% end %>
</select>
This will display all of the artists in a drop-down box. Then, when the form is submitted, the id of the artist that is selected (as well as any other fields that you set up) will be accessible through @params["new_song"].
Since each song also belongs to an album, I’d like to have the ability for the user to also select what album the song belongs to when adding the song. Ideally, when an artist is selected, only the albums belonging to that artist would appear in the albums drop-down. To avoid using JavaScript to limit which albums appear, I feel that the form will have to post back to the server in order to accomplish this. So, my next step in getting this lyrics web app going is to figure that out.
subscribe to comments RSS
There are no comments for this post