I’m trying to make the “home” link in my <%= render 'layouts/header' %> do an ajax/jquery call to change the <%= yield %> in a partial inside my content div. all i get are blanks in the view.. <%= yield %> works fine when put in a partial without ajax, but it doesn’t display anything when using ajax… can yield not be used this way?
all I’m really looking for is the ability to click on my sites navigation links without having to reload the entire page…
my application.html.erb file looks like so:
<head>
$(function() {
$("#home").live("click", function() {
$.get(this.href, null, null, "script");
return false; }); });
</head>
<body>
<div id="container">
<%= render 'layouts/header' %>
<div id="content">
<%= render 'layouts/content' %>
</div>
<%= render 'layouts/footer' %>
</div>
</body>
my <%= render 'layouts/header' %> contains:
<%= link_to "Home", root_path, :id => "home" %>
my <%= render 'layouts/content' %> only contains:
<%= yield %>
home.js.erb
$("#content").html("<%= escape_javascript(render("layouts/content")) %>");
In
home.js.erbyou are rendering an empty layout file. Try rendering some view instead.You can think of
<%= yield %>as a placeholder for rendered view content in a layout file. If you render just the layout, there’s nothing to be put in there.http://guides.rubyonrails.org/layouts_and_rendering.html#understanding-yield
Alternative solution
Add this to top of your controller to disable layouts for ajax requests:
This will make all links inside of
#headerload with ajax and place the result to#contentdiv:Remove js format from your controller actions because that’s not needed anymore.