我希望我的轮播仅出现在 rails 应用程序的索引页面中

I want my carousel to be appeared in index page only in rails application

我是 rails 的新手,我希望我的轮播代码只在索引页中看到,请给出解决方案,下面是我的代码:

<body>
  <%= render 'layouts/navigation'%>
  <%= render 'layouts/carousel' %>
  <%= render 'layouts/messages'%>

  <div class="container">
    <div class="row">
      <%= yield %>
    <div class="col-md-8">
    </div>
    <div class="col-md-4">
  </div>
</body>

通常情况下,您希望将任何特定于页面的代码移出布局并移入模板以执行适当的操作。

但是,在您的情况下,那段代码看起来嵌入了页面框架的其余部分,因此可能很难以这种方式重构它。

出于这个原因,我建议通过简单地使用布尔变量来使用条件渲染。

例如你可以这样做:

<%= render 'layouts/carousel' if @render_carousel %>

然后您可以将 @render_carousel 变量设置为 true 以用于您希望轮播出现的任何操作,例如:

def index
    # rest of the code
    @render_carousel = true
end