1 Replies - 3073 Views - Last Post: 25 October 2012 - 07:32 AM Rate Topic: -----

#1 SanmanX  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 32
  • Joined: 11-January 12

How to render my ajax data

Posted 24 October 2012 - 02:59 PM

Hi guys I'm having issues rendering my ajax data into a div and needed some help

here is the .js file:
	function ajaxSearch(){
			
		$("#prod").keyup(function(){

          var product_id = $("#prod").val();
          
          	ajaxObject =$.ajax({
          		type:"GET",
          		dataType:'json',
          		url:'/products/product_info',
          		data:{product_id: product_id}
          
          	});
           var results = jQuery.parseJSON(ajaxObject.responseText);
           $("#results").html(results);
		});

	};



in my controller:

 def product_info
    if params[:product_id]
      @product = Product.find_by_id(params[:product_id])
      puts "this is the product title : #{@product.title}"
      respond_to do |format|
      format.js {render json: @product}
    end
    end
  end


and the html part:
<form action="/products/product_info" onsubmit="return false;" onkeyup="ajaxSearch(); return false">
  <input id ="prod" type="text" value="" placeholder="some text..."/>

</form>

<div id="results"></div>


When i look at console in the terminal everything is fine, ajax is working and its getting the correct products (the puts is in there to test it was working), but i'm sure how to render. I tried doing a render :partial => /products/show but in the terminal it kept saying missing template. which didn't make sense to me because rails automatically makes that for you. So I am lost right now.

Is This A Good Question/Topic? 0
  • +

Replies To: How to render my ajax data

#2 SanmanX  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 32
  • Joined: 11-January 12

Re: How to render my ajax data

Posted 25 October 2012 - 07:32 AM

found one way to get it to work, i switched from .ajax to .get:

function ajaxSearch(){
		
	$("#prod").keyup(function(){

         var product_id = $("#prod").val();
           $.get('products/product_info',
             {product_id: product_id},
             function(result){
          $("#results").html(result.id);
          }
	});

};



hope that helps anyone else with this issue.

This post has been edited by SanmanX: 25 October 2012 - 07:37 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1