Created
October 19, 2012 15:56
-
-
Save jorgegit/3919002 to your computer and use it in GitHub Desktop.
View of the dynamic drop down example with Rails, Coffeescript and jQuery
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
jQuery -> | |
$('#knowledge_parent_subject').change -> | |
selected_id = $('#knowledge_parent_subject option:selected').val() | |
$.get "/subjects/#{selected_id}/subcategories" |
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
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 |
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
<%= 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 %> |
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
$("#knowledge_subject").html('<%=j options_from_collection_for_select(@subcategories, "id", "name") %>'); |
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
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