Rails, update 2nd select with static data from partial, based on initial select
I have a rails form where the end user is going to drop down a list of categories, select a category.开发者_开发技巧 I need this selection to update the next select drop down. The issue is that the 2nd select needs to be populated by static data from a partial.
The reason for this, is that its a product registration page, that includes more options than just those living in the db. In fact hardly any of the data is in the db, so it will be easier to just setup 5 lists in partials, that are loaded depending on the category selection.
Just not sure how to set this up. I am using Jquery 1.3.2 and Rails 2.3.3
Any help is greatly appreciated.
Check out the ActiveScaffold plugin. It streamlines this process. You want the bit on chaining form fields.
Since you have jQuery as one of your tags I'd go the jQuery route. First select the correct function - if you're just bringing back data then JSON makes the most sense to me. If you want return a list of option tags then a regular get will work.
Here's some pseudo code using get:
$(function() {
$("#category-select").change(function() {
var id = $("#category option:selected").val(); // assuming the value is the category id
var url = "/categories/" + id + "/subcategories/";
.get(url, function(data) {
$("#product-select").html(data); // assumes data is a bunch of <option> tags
});
});
});
精彩评论