Skip to content

Instantly share code, notes, and snippets.

@pullmonkey
pullmonkey / gist:3374533
Created August 16, 2012 23:40
the erb version of dynamic select box js updater for rails 3
# 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)) %>");
@pullmonkey
pullmonkey / gist:3366572
Created August 16, 2012 03:56
erb alternative for the dynamic select box tutorial
# 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() {
@pullmonkey
pullmonkey / gist:3319709
Created August 11, 2012 01:10
dynamic select routes
# 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
@pullmonkey
pullmonkey / gist:3319706
Created August 11, 2012 01:10
dynamic select home updating rjs
# 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))}");
@pullmonkey
pullmonkey / gist:3319691
Created August 11, 2012 01:08
dynamic select home index view
# 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() {
@pullmonkey
pullmonkey / gist:3319690
Created August 11, 2012 01:07
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
@pullmonkey
pullmonkey / gist:3319683
Created August 11, 2012 01:05
dynamic select models
# 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
@pullmonkey
pullmonkey / gist:3319679
Created August 11, 2012 01:04
dynamic select seed
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
@pullmonkey
pullmonkey / gist:3319671
Created August 11, 2012 01:03
dynamic select setup
# 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
@pullmonkey
pullmonkey / gist:3318540
Created August 10, 2012 22:09
Dynamic Select Box Rails code
# 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