如何使用 rails 3 在 jQuery ajax 成功方法上渲染部分

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11844513/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 10:58:58  来源:igfitidea点击:

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

jqueryruby-on-rails-3ruby-on-rails-3.2

提问by manish nautiyal

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

我正在使用带有 jQ​​uery 的 rails 3.2.1 进行 ajax 调用。

My jQuery code is :

我的 jQuery 代码是:

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

它应该如何显示我的渲染部分页面。:(

回答by PradeepKumar

try by rendering the partial from the controller as,

尝试将控制器中的部分渲染为,

def method_name
  render :partial => 'some_partial'
end

and in js,

在 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:

创建一个与该操作对应的 js.erb 文件,并在其中粘贴以下内容:

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

回答by Dougui

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

您可以创建此文件:/app/views/org_pages/index.js.erb 并将其放入您的文件中:

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

This code will be rendered and executed on success.

此代码将在成功时呈现并执行。