How to do render partial on jQuery ajax success method with rails 3

丶灬走出姿态 提交于 2019-12-03 03:20:47

问题


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


回答1:


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')) %>");



回答2:


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.



来源:https://stackoverflow.com/questions/11844513/how-to-do-render-partial-on-jquery-ajax-success-method-with-rails-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!