好的,所以我使用下面的代码来获取标签列表并将其加载到 select2 框中。返回的选项["test1","test2"]
应该是正确的格式,但我假设它们需要以某种方式循环处理。
//This part is meant to grab the options. I am using model ID 473 for testing
$('#ticket_style_id').on("change", function(e) {
var tag_list = $.ajax({
url: "/grab_options/<%= 473 %>",
async: false
}).responseText;
//This part is meant to load the tag_list into a select2 box based on the
//selection above
$("#ticket_option_list").select2({
tags: [ tag_list ]
});
})
有趣的是,如果我替换以下内容:
$("#ticket_option_list").select2({
tags: ["test1","test2"]
});
...一切都很好。
此控制器代码正在返回 JSON:
def grab_options
style = Style.find(params[:id])
respond_to do |format|
format.js { render json: style.option_list.to_json }
end
end