将<%=产量%>在 ajax jquery 调用的部分内容中
我正在尝试在 <%= render 'layouts/header' %>
中创建“home”链接,执行 ajax/jquery 调用来更改 <%= 产量%>
在我的内容 div 内的部分内容中。我得到的只是视图中的空白.. <%= Yield %>
在没有 ajax 的情况下放入部分时工作正常,但在使用 ajax 时它不会显示任何内容...可以 < code>yield 不能这样使用吗?
我真正想要的是能够单击我的网站导航链接,而无需重新加载整个页面...
我的 application.html.erb 文件如下所示:
<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>
我的 <%= render 'layouts/ header' %>
包含:
<%= link_to "Home", root_path, :id => "home" %>
我的 <%= render 'layouts/content' %>
仅包含:
<%= yield %>
home.js.erb
$("#content").html("<%= escape_javascript(render("layouts/content")) %>");
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")) %>");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的用例完全由 rails-ajax gem 解决。
您所要做的就是使用
div#content
配置它作为其主要默认容器,您的网站将使用此容器自动进行 ajaxified。无需更改应用程序的代码。它为您处理了很多麻烦:书签、历史记录、脚本执行、重定向、表单、禁用 JS 的浏览器的回退...
您还可以指定要在某些特定控制器的操作上刷新的其他部分。
Your usecase is exactly tackled by the rails-ajax gem.
All you have to do is configure it with
div#content
as its main default container, and your website will be automatically ajaxified using this container. No need to alter your application's code.It handles a lot of hassle for you: bookmarking, history, scripts executions, redirections, forms, fallback for JS-disabled browsers...
You can also specify additional partials to be refreshed on some specific controllers' actions.
在
home.js.erb
中,您正在渲染一个空布局文件。尝试渲染一些视图。您可以将
<%= yield %>
视为布局文件中渲染视图内容的占位符。如果您仅渲染布局,则无需在其中放置任何内容。http://guides.rubyonrails.org/layouts_and_rendering.html#understand-yield
替代解决方案
将其添加到控制器顶部以禁用 ajax 请求的布局:
这将使
#header
内的所有链接都使用 ajax 加载并将结果放置到#content
div:从控制器操作中删除 js 格式,因为不再需要它了。
In
home.js.erb
you 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
#header
load with ajax and place the result to#content
div:Remove js format from your controller actions because that's not needed anymore.