使用 rjs 进行 link_to_remote 的混淆
我的应用程序布局包含一个导航 div 和一个构建为部分的内容 div。我想使用 ajax,以便每当用户单击导航 div 中的链接时,该页面的内容就会在内容 div 中呈现,而无需刷新。我对如何正确执行此操作感到困惑...对 Rails 菜鸟有什么帮助吗???先谢谢了~
application.html.erb
<body>
<div id="container">
<%= render 'layouts/header' %>
<%= render 'layouts/content'%>
<%= render 'layouts/footer' %>
</div>
</body>
_header.html.erb
<%= link_to_remote "Home", :url => { :controller => "pages", :action => "home" } %>
_content.html.erb
<div id="content">
<%= yield %>
</div>
Pages_controller.rb
def home
@title = "Home"
respond_to do |format|
format.js
end
end
home.rjs
page.replace_html :container, :partial => 'layouts/content'
My application layout contains a navigation div, and a content div constructed as a partial. I want to use ajax so that whenever a person clicks on a link in the navigation div, the contents of that page renders in the content div without a refresh. I'm confused on how to properly do this... any help for a rails noob??? thanks in advance~
application.html.erb
<body>
<div id="container">
<%= render 'layouts/header' %>
<%= render 'layouts/content'%>
<%= render 'layouts/footer' %>
</div>
</body>
_header.html.erb
<%= link_to_remote "Home", :url => { :controller => "pages", :action => "home" } %>
_content.html.erb
<div id="content">
<%= yield %>
</div>
pages_controller.rb
def home
@title = "Home"
respond_to do |format|
format.js
end
end
home.rjs
page.replace_html :container, :partial => 'layouts/content'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试将“home.rjs”重命名为“home.js.rjs”。其他一切看起来都是正确的。你遇到什么错误?
Try renaming 'home.rjs' to 'home.js.rjs'. Everything else looks correct. What error are you getting?
尝试:
pages_controller.rb
您不需要额外的 rjs 文件。另外仅供参考,如果您将@title保存到数据库,那么您需要在respond_to之前执行@title.reload以获取最新数据。上面关于萤火虫的评论很有帮助。
Try:
pages_controller.rb
You don't need extra rjs file. Also FYI, if you are saving @title to the DB, then you need to do @title.reload before respond_to to get latest data. The comment above about firebug is helpful.