This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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)) %>"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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'}) %> | |
<script> | |
$(document).ready(function() { | |
$('#genres_select').change(function() { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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))}"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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() { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
NewerOlder