【发布时间】:2021-08-28 18:56:07
【问题描述】:
我有一个 _topbar.html.erb 部分元素,用于我从主页的索引中呈现的元素。
我想在其他页面中调用相同的部分,但根据渲染它的端点,我想显示不同的内容。
有什么方法可以根据 Rails 上的渲染源应用动态内容?
【问题讨论】:
标签: html ruby-on-rails render erb partial
我有一个 _topbar.html.erb 部分元素,用于我从主页的索引中呈现的元素。
我想在其他页面中调用相同的部分,但根据渲染它的端点,我想显示不同的内容。
有什么方法可以根据 Rails 上的渲染源应用动态内容?
【问题讨论】:
标签: html ruby-on-rails render erb partial
一种方法是使用current_page? 所以你可以这样做:
<!-- _topbar.html.erb -->
<% if current_page?(posts_path) %>
Here are my posts
<% else %>
Check out my <%= link_to "posts page", posts_path %>
<% end %>
(文档为here)
【讨论】: