1

I have this function in my controller:

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

And I'm making this AJAX call:

 $(".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);
    }

 );

It's going through the controller function fine, but for some reason, I'm not getting back the HTML I want from the call. I'm getting nothing at all. Can someone help me out?

1 Answer 1

2

if you are sending html in the resp then set the dataType accordingly

$.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
Sign up to request clarification or add additional context in comments.

2 Comments

Aha, that did the trick! Thank you! There really is no need to do an embedded <%= @html %> is there?
i really cant say but apparently there is no need

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.