Skip to content

Instantly share code, notes, and snippets.

@pullmonkey
Created August 11, 2012 01:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pullmonkey/3319690 to your computer and use it in GitHub Desktop.
Save pullmonkey/3319690 to your computer and use it in GitHub Desktop.
dynamic select home controller
# app/controllers/home_controller.rb
class HomeController < ApplicationController
def index
@genres = Genre.all
@artists = Artist.all
@songs = Song.all
end
def update_artists
# updates artists and songs based on genre selected
genre = Genre.find(params[:genre_id])
# map to name and id for use in our options_for_select
@artists = genre.artists.map{|a| [a.name, a.id]}.insert(0, "Select an Artist")
@songs = genre.songs.map{|s| [s.title, s.id]}.insert(0, "Select a Song")
end
def update_songs
# updates songs based on artist selected
artist = Artist.find(params[:artist_id])
@songs = artist.songs.map{|s| [s.title, s.id]}.insert(0, "Select a Song")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment