19
Models with Associations
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:
Have you found this post helpful?
If so, I'd appreciate if you could indicate so by pressing the Google Plus button below.
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.
Related posts:
Post comment
Recommended Services
Recent Posts
- Fantastic new corporate themes for WordPress
- Vistaprint offers FREE t-shirts, too!
- 80+ “Your Ad Here” 125 x 125 banners
- 5 Minute Long Tail SEO Drill: More Traffic, Better SERPs
- iPhone and iPod Touch app statistics: OS adoption, purchasing rates
Recent Comments
- Neerav on Using JavaScript to validate one or more grouped checkboxes
- Jacob on Using JavaScript to validate one or more grouped checkboxes
- kaify on Using JavaScript to validate one or more grouped checkboxes
- JC Goldenstein on How To: Cloak your Affiliate Links for Free in Under 3 Seconds
- Bail Bonds Los Angeles on Amazon Web Services on Rails
Categories
- .net
- acoustic guitar
- affiliate marketing
- ajax
- amazon associates
- blogging
- books
- business ideas
- c#
- code igniter
- dealdotcom
- google adsense
- google adwords
- internet marketing
- iPhone
- javascript
- leadership
- make money online
- mortgage goal
- msn adcenter
- networking
- personal development
- php
- ppc
- ruby
- ruby on rails
- seo
- text-link-ads
- web development
- yahoo search marketing


