public
Created

Dynamic Select Box Rails code

  • Download Gist
gistfile1.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
# create new rails app
# rails new dynamic_select_boxes -m https://raw.github.com/RailsApps/rails3-application-templates/master/rails3-haml-html5-template.rb
 
# create models:
# rails g model genre name:string
# rails g model artist name:string genre_id:integer
# rails g model song title:string artist_id:integer
 
# rake db:migrate
 
# db/seeds.rb
3.times do |x|
genre = Genre.find_or_create_by_name(:name => "Genre #{x}")
3.times do |y|
artist = Artist.find_or_create_by_name(:name => "Artist #{x}.#{y}", :genre => genre)
3.times do |z|
Song.find_or_create_by_title(:title => "Song #{x}.#{y}.#{z}", :artist => artist)
end
end
end
 
# app/models/artist.rb
class Artist < ActiveRecord::Base
belongs_to :genre
 
has_many :songs
 
attr_accessible :genre_id, :name, :genre
end
 
# app/models/genre.rb
class Genre < ActiveRecord::Base
attr_accessible :name
 
has_many :artists
has_many :songs, :through => :artists
end
 
# app/models/songs.rb
class Song < ActiveRecord::Base
belongs_to :artist
 
attr_accessible :artist_id, :title, :artist
end
 
# 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
 
# app/views/home/index.html.haml
= collection_select(nil, :genre_id, @genres, :id, :name, {:prompt => "Select a Genre"}, {:id => 'genres_select'})
%br
= collection_select(nil, :artist_id, @artists, :id, :name, {:prompt => "Select an Artist"}, {:id => 'artists_select'})
%br
= collection_select(nil, :song_id, @songs, :id, :title, {:prompt => "Select a Song"}, {:id => 'songs_select'})
 
:javascript
$(document).ready(function() {
$('#genres_select').change(function() {
$.ajax({
url: "#{update_artists_path}",
data: {
genre_id : $('#genres_select').val()
},
dataType: "script"
});
});
$('#artists_select').change(function() {
$.ajax({
url: "#{update_songs_path}",
data: {
artist_id : $('#artists_select').val()
},
dataType: "script"
});
});
});
 
# app/views/home/update_artists.js.haml
$('#artists_select').html("#{escape_javascript(options_for_select(@artists))}");
$('#songs_select').html("#{escape_javascript(options_for_select(@songs))}");
 
# app/views/home/update_songs.js.haml
$('#songs_select').html("#{escape_javascript(options_for_select(@songs))}");
 
# config/routes.rb
DynamicSelectBoxes::Application.routes.draw do
get 'home/update_artists', :as => 'update_artists'
get 'home/update_songs', :as => 'update_songs'
root :to => "home#index"
end
 
# rake db:seed
 
# rails s

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.