如何根据渲染调用的来源动态渲染 html.erb 部分?

问题描述

我有一个 _topbar.html.erb 部分,其中包含我从主页索引呈现的元素。 我想在其他页面调用相同的部分,但根据呈现它的端点,我想显示不同的内容

有什么方法可以根据 Rails 上的渲染源应用动态内容吗?

解决方法

一种方法是使用 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 %>

(文档为 Firebase Query Double Nested