【发布时间】:2018-11-19 10:52:37
【问题描述】:
我正在编写一个 Rails 应用程序。我想在索引页面的整个 HTML 标记中使用特定样式,但其余页面应遵循应用程序布局。所以,在我的控制器中,我有:
def index
render layout: 'index_layout'
end
# All other actions follow this scheme
def any_other_action
end
索引布局有一段特定的代码可以改变样式:
<!-- Index layout -->
<html id="index_html">
...
<!-- Application layout (goes normally) -->
<html>
...
但是,当我通过 Rails 中的 link_to 标记从一个窗口切换到另一个窗口时,布局没有被刷新,并且视图保持上一页中分配的 id,例如,如果我从 index 转到 any其他页面,html 标记的 id 为 index_html,反之亦然。但是,如果我执行完全刷新(按 F5 键),布局会正确呈现。我在 Mozilla Firefox 和 Chrome 中进行了测试,以检查这是否是浏览器特有的问题,但结果是相同的。
当我通过link_to标签导航到任何其他页面然后重新加载这个新页面之后,我的服务器会在我请求索引页面时产生这个输出:
Processing by HomeController#index as HTML
Rendering home/index.html.haml within layouts/index_layout
Rendered home/index.html.haml within layouts/index_layout (9.9ms)
Rendered layouts/_navbar.html.haml (13.6ms)
Completed 200 OK in 1254ms (Views: 1201.9ms | ActiveRecord: 0.0ms)
# Navigation with link_to tag, the index_html id is still appearing
Started GET "/community" for 127.0.0.1 at 2018-06-09 11:45:58-0500
Processing by HomeController#community as HTML
Rendering home/community.html.haml within layouts/application
Rendered home/_community_members_panel.html.haml (6.1ms)
Rendered home/_community_companies_panel.html.haml (8.1ms)
Rendered home/community.html.haml within layouts/application (41.5ms)
Rendered layouts/_navbar.html.haml (10.6ms)
Completed 200 OK in 110ms (Views: 101.0ms | ActiveRecord: 0.0ms)
# Full refresh of the page with F5 key, the index_html id doesn't appear, as it should.
Started GET "/community" for 127.0.0.1 at 2018-06-09 11:46:49 -0500
Processing by HomeController#community as HTML
Rendering home/community.html.haml within layouts/application
Rendered home/_community_members_panel.html.haml (5.6ms)
Rendered home/_community_companies_panel.html.haml (6.6ms)
Rendered home/community.html.haml within layouts/application (19.4ms)
Rendered layouts/_navbar.html.haml (8.4ms)
Completed 200 OK in 74ms (Views: 72.7ms | ActiveRecord: 0.0ms)
谁能给我一些关于可能失败的信息?我的直觉是关于缓存的,但我找不到任何可以帮助我的答案。我的 Ruby 版本是 2.5.1,我的 rails 版本是 5.1.6
【问题讨论】:
-
也许这会有所帮助:stackoverflow.com/a/3025806/5239030
-
感谢您的回答,@iGian,但问题不在于呈现错误的布局,正在呈现正确的布局(正如服务器的输出所说),但没有重绘,或者重新加载
标签: html ruby-on-rails caching layout