Jquery 将AJAX与RubyonRails结合使用

Jquery 将AJAX与RubyonRails结合使用,jquery,ruby-on-rails,ajax,json,Jquery,Ruby On Rails,Ajax,Json,我的控制器中有以下功能: def showRecipeIngredients ingredients = RecipeIngredient.where( "recipe_id = ?", params[:id]).all @html = "" ingredients.each do |i| @html = @html + "<p>#{Food.find_by_ndb_no(i.ndb_no).long_desc} - #{i.quantity}

我的控制器中有以下功能:

def showRecipeIngredients
    ingredients = RecipeIngredient.where( "recipe_id = ?", params[:id]).all
    @html = ""
    ingredients.each do |i|
        @html = @html + "<p>#{Food.find_by_ndb_no(i.ndb_no).long_desc} - #{i.quantity} #{i.units}</p>"
    end


# value of @html: <p>Cheese, parmesan, grated - 6.0 cup</p><p>Cheese, port de salut - 8.0 cup, diced</p><p>Salad dressing, russian dressing, low calorie - 45.0 tablespoon</p>

    result = @html
    render( :json => result )
end
def显示接收元件
配料=配方成分。其中(“配方id=?”,参数[:id])。全部
@html=“”
成分。每种都可以|
@html=@html+“{Food.find_by_ndb_no(i.ndb_no).long_desc}-{i.quantity}{i.units}

” 结束 #@html:奶酪、帕尔玛干酪、磨碎-6.0杯奶酪、礼炮港-8.0杯、切丁沙拉酱、俄罗斯酱、低热量-45.0汤匙

结果=@html 呈现(:json=>result) 结束
我正在做一个AJAX调用:

 $(".recipe_ids").click(function() {

    var id = parseInt( $(this).attr('id').substring(11) );

    alert("THIS ID: " + id);

    $.get( "/recipes/showRecipeIngredients?id="+id, function(result) {

 //things get weird over here
        var obj = jQuery.parseJSON(result);
        var obj2 = <%= @html %>

        $("#search_results").html(result);
    }

 );
$(“.recipe_id”)。单击(函数(){
var id=parseInt($(this).attr('id').substring(11));
警报(“此ID:+ID”);
$.get(“/recipes/showRecipeIngElements?id=”+id,函数(结果){
//这里的事情变得很奇怪
var obj=jQuery.parseJSON(结果);
变量obj2=
$(“#搜索结果”).html(结果);
}
);

它可以通过控制器功能正常运行,但由于某些原因,我无法从调用中返回所需的HTML。我什么也没有得到。有人能帮我吗?

如果您在resp中发送HTML,请相应地设置数据类型

$.get( "/recipes/showRecipeIngredients?id="+id, function(result) {

        //no need to parse json if its not json
        //var obj = jQuery.parseJSON(result);
        var obj2 = <%= @html %>

        $("#search_results").html(result);
    },'html');//<-- set the dataType
$.get(“/recipes/showRecipeIngElements?id=“+id,函数(结果){
//如果不是json,则无需解析json
//var obj=jQuery.parseJSON(结果);
变量obj2=
$(“#搜索结果”).html(结果);

},'html');//啊哈,真的成功了!谢谢!真的没有必要做嵌入式
是吗?我真的不能说,但显然没有必要