Skip to content

Instantly share code, notes, and snippets.

@jorgegit
Created October 19, 2012 15:56
Show Gist options
  • Save jorgegit/3919002 to your computer and use it in GitHub Desktop.
Save jorgegit/3919002 to your computer and use it in GitHub Desktop.
View of the dynamic drop down example with Rails, Coffeescript and jQuery
jQuery ->
$('#knowledge_parent_subject').change ->
selected_id = $('#knowledge_parent_subject option:selected').val()
$.get "/subjects/#{selected_id}/subcategories"
def new
# Find top subjects
@parent_subjects = Subject.find_all_by_parent_id(nil)
# Find subjects for the first parent subject
@subjects = Subject.find_all_by_parent_id(@parent_subjects.first.id)
@knowledge = Knowledge.new()
end
<%= simple_form_for @knowledge,:url => professor_knowledges_path, :html => { :class => 'form-horizontal' } do |f| %>
<div class="control-group select optional">
<%= label_tag "Subject Type", nil, :class => "select optional control-label"%>
<div class="controls">
<%= select_tag "Parent Subject", options_from_collection_for_select(@parent_subjects, "id", "name"), :id => "knowledge_parent_subject" %>
</div>
</div>
<%= f.input :subject, :collection => @subjects, :label => "Subject", :selected => @subjects.first %>
<%= f.button :submit, t('add_form'),:class => 'btn-primary' %>
<% end %>
$("#knowledge_subject").html('<%=j options_from_collection_for_select(@subcategories, "id", "name") %>');
class SubjectsController < ApplicationController
def subcategories
respond_to do |format|
format.js { @subcategories = Subject.find_all_by_parent_id(params[:id]) }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment