[英]How to make asynchronous GET request on RAILS and only get view form (not whole body and headers)
I created a simple asynchronous jquery function on my rails app so I can print the response on a "display" div. 我在rails应用程序上创建了一个简单的异步jquery函数,因此可以在“ display” div上打印响应。 But when I make the GET request to my rails url it responds the whole html: "body", "head" and "includes" as if it were a new page on a browser, so the resulting source is the following: 但是,当我向rails URL发出GET请求时,它会响应整个html:“ body”,“ head”和“ includes”,就好像它是浏览器上的新页面一样,因此,结果如下:
Before I push the button: 在按下按钮之前:
<html>
<head>
<title>Myapp1</title>
</head>
<body>
<button onclick="callPage()">Call</button>
<div id="display"></div>
</body>
<html>
After I push the button and the "display" div is filled with my response: 在按下按钮后,“ display” div充满了我的响应:
<html>
<head>
<title>Myapp1</title>
</head>
<body>
<button onclick="callPage()">Call</button>
<div id="display">
*<html>
<head>
<title>Myapp1</title>
</head>
<body>
-----Content of page user/1 which is the only thing I want to get.------
</body>
<html>*
</div>
</body>
My route to users on routes.rb (RUBY ON RAILS) working fine 我在route.rb(RUBY ON RAILS)上通往用户的路线正常
resources :users
#get 'welcome/index'
match ':controller(/:action(/:id))', :via => :get
My Jquery GET callPage (working fine) 我的jQuery GET callPage(工作正常)
<script>
function callPage()
{
url="user/1";
div="display";
$.ajax({
type: "GET",
dataType: "text",
url: url,
success: function(msg){
if(parseInt(msg)!=0)
{
$("#"+div).html(msg);
}
}
}).setRequestHeader('X-CSRF-Token', AUTH_TOKEN);
}
</script>
The question is how do I prevent RAILS from printing out every html tag all over (body, heads,...) and get it to print only the view of the controller I want (in this case, users). 问题是如何防止RAILS将所有html标记全部打印出来(身体,头部等),并使其仅打印我想要的控制器(在这种情况下为用户)的视图。 ¿Is it a configuration somewhere or do I wave to trim this off somehow? ¿它是某处的配置吗?还是我想以某种方式调整它?
Thanks a million in advance for your time. 预先感谢您花费一百万美元。
To only display the view you have to change your controller action to have render layout: false at the end. 要仅显示视图,您必须将控制器操作更改为具有渲染布局:最后为false。 This will prevent the layout from rendering, leaving only the content in your view file. 这将阻止呈现布局,仅将内容保留在视图文件中。
For example: 例如:
class YourController < ApplicationController
def show
# bla bla
render 'your_show_view_name', layout: false
end
end
If you want all your controller actions to have this behavior you can put it right at the top 如果您希望所有控制器动作都具有此行为,则可以将其放在顶部
class YourController < ApplicationController
layout false
def show
# bla bla
end
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.