【问题标题】:How to render html.erb partial dynamically depending on the origin of the render call?如何根据渲染调用的来源动态渲染 html.erb 部分?
【发布时间】:2021-08-28 18:56:07
【问题描述】:

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

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

【问题讨论】:

    标签: html ruby-on-rails render erb partial


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多