I created a simple asynchronous jquery function on my rails app so I can print the response on a "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:
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:
<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
resources :users
#get 'welcome/index'
match ':controller(/:action(/:id))', :via => :get
My Jquery GET callPage (working fine)
<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). ¿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. 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
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments