how to do render partial on jquery ajax success method with rails 3

I'm using rails 3.2.1 with jquery for an ajax call.

My juqery code is :

jQuery.ajax({                 url: "/org_pages",                 data: 'org_id='+ org_id,                 type: "POST",                 success: function(result){                     jQuery("#image_center").html("<%= escape_javascript(render(:partial => 'pages/top_link')) %>");                 },                 error: function(){                     alert('Error occured');                 }             });

My problem is on the web page the output is showing this :

<%= render :partial => 'pages/top_link', :collection=>@pages %>

How it should display my render partial page. :frowning:

I'm using rails 3.2.1 with jquery for an ajax call.

My juqery code is :

jQuery.ajax({                url: "/org_pages",                data: 'org_id='+ org_id,                type: "POST",                success: function(result){                    jQuery("#image_center").html("<%= escape_javascript(render(:partial => 'pages/top_link')) %>");                },                error: function(){                    alert('Error occured');                }            });

My problem is on the web page the output is showing this :

<%= render :partial => 'pages/top_link', :collection=>@pages %>

How it should display my render partial page. :frowning:

What is the file-extension of the script you posted? Does it include .erb as the last segment? If not, then the erb code won't be evaluated at all.

Walter

That is a .js file.

You need to know the flow first

if u write .erb file it will execute by interpreter and only html response will served to client side browser .

But you are trying to execute the ruby code from javascript which will not possible. so it is considering as text and displaying that.

anyway request is going to server, so my advise is run the required ruby code on server and the response html will be served to you

for that you need to create the request_getting_method.js.erb on server and place your code on that like this

$(“#image_center”).html(“<%=escape_javascript(render(:partial => ‘pages/top_link’)) %>”);

so that will execute on server and gives responce to client and the responce will replace by image_cente id element html content.

That is a .js file.

And so, if you want it to also include interpolation, using erb syntax, you have to tack .erb to the end of the filename, and you have to put the file in a location where it will be interpreted (hint: not in /public/javascripts).

But step back and think if there's another way to write your JavaScript so you don't need that interpolation. Can you write that variable data into the partial for /org_pages? The reason you would want to do that is so that the JavaScript can be compressed and minified along with all the other static assets. The moment you start generating your JavaScript dynamically, you force the browser to re-load it from scratch each page load, along with all the HTML. Your Ajax call is already creating a dynamic snippet of HTML -- what's a little more HTML there?

Walter