Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 jQuery 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. :(

like image 702
manish nautiyal Avatar asked Aug 07 '12 11:08

manish nautiyal


2 Answers

try by rendering the partial from the controller as,

def method_name
  render :partial => 'some_partial'
end

and in js,

success: function(result){                      
  jQuery("#image_center").html(result);
}

OR ELSE

create a js.erb file corresponding to that action and inside that paste the following:

jQuery("#image_center").html("<%= escape_javascript(render(:partial => 'pages/top_link')) %>");
like image 169
PradeepKumar Avatar answered Nov 15 '22 21:11

PradeepKumar


You could create this file : /app/views/org_pages/index.js.erb and put this in your file :

jQuery("#image_center").html("<%= escape_javascript(render(:partial => 'pages/top_link')) %>");

This code will be rendered and executed on success.

like image 40
Dougui Avatar answered Nov 15 '22 22:11

Dougui