【问题标题】:How to skip the content_for tag content for some pages in rails?如何跳过rails中某些页面的content_for标签内容?
【发布时间】:2012-10-30 13:57:39
【问题描述】:

我有一个布局,我在其中使用 content_for 标记来呈现一些部分(用于侧边栏)。但是在几个页面上我不需要这个侧边栏。那么问题来了:我怎样才能告诉 rails 不要为某些页面渲染任何东西呢?

我尝试了以下方式但没有任何结果:

<% content_for :sidebar do %>
  <%= render nothing: true %>
<% end %>

刚刚出错

You invoked render but did not give any of :partial, :template, :inline, :file or :text option.

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-3 views content-for


    【解决方案1】:

    不要在不想看到内容的视图中指定 content_for

    【讨论】:

    • 就是这样。为应消除侧边栏的页面指定新布局。
    • 另外——如果你需要知道内容是否被指定,你可以运行&lt;% if content_for?(:sidebar) %&gt;
    【解决方案2】:
     <%= render text: "&nbsp;" %>
    

    【讨论】:

    • 问题是 :sidebar 已经有一些内容了。所以这段代码只是追加了新的文本。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-01
    • 1970-01-01
    相关资源
    最近更新 更多