Every Ingredient has one Category.
First a user selects a Category then
store all ingredients of that category into a second select field.
<%= select("category", "category_id", Category.all.collect {|c| [ c.name, c.id ] }) %>
My approach is to update the second select field with ajax, but once placing the ajax call after the alert statement the whole function is not called anymore.
$(document).ready(function(){
$(document).on('change', '#category_category_id', function(){
alert("changed");
$.ajax({
url: "categories/category_selection",
type: "GET",
data: {'ingredient=' + $('#ingredient_selection option:selected').value() },
})
});
});
Same with
$('#category_category_id').live('change', function(){...})
Categories_controller.rb
class CategoriesController < ApplicationController
def category_selection
respond_to do |format|
format.js { }
end
end
end
Category_selection.js.erb
$('#category_category_id').after('
<%= select_tag :category_selection,
options_from_collection_for_select(@ingredients, :id, :name),
:prompt => "Ingredient" %>
');

$('#ingredient_selection option:selected').value()