【问题标题】:Rails content_for not rendering partialRails content_for 不渲染部分
【发布时间】:2017-06-01 15:58:50
【问题描述】:

由于某种原因,我无法在 content_for 块内渲染部分内容。

代码如下:

/ => index.html.slim
doctype html
html lang="de"
  = render "layouts/headinclude"
  body
    = yield
    - if Rails.env.production?
      - content_for :additional_headincludes do
        = render "layouts/hotjar"

由于某种原因,以下不包括我的部分完全渲染:

  / # => _headinclude.html.slim
  head
    title= @title || "My Title"
    link href='//fonts.googleapis.com/css?family=Droid+Sans:400,700' rel='stylesheet' type='text/css'
    - if content_for?(:additional_headincludes)
      = yield :additional_headincludes

我看不出这不起作用的原因。任何帮助表示赞赏。 当直接在我的headinclude-partial 中渲染部分时,一切正常。

【问题讨论】:

    标签: ruby-on-rails rendering partial content-for


    【解决方案1】:

    这里的问题在于订单。 在调用 render "layouts/headinclude" 之前,我必须定义 content_for-block。

    请注意,如果“回答”content_for-block(包含 render "layouts/hotjar"-part 的那个)位于模板内(如 showindex 或您在任何模板中,则此概念会起作用目前是)。原因是 Rails 解析内容的顺序。

    模板似乎在布局之前得到解析,因此在这种情况下,“询问”content_for-block 将有实际数据要显示。

    这是(一个可能的)答案:

    / => index.html.slim
    - if Rails.env.production?
      - content_for :additional_headincludes do
        = render "layouts/hotjar"
    doctype html
    html lang="de"
      = render "layouts/headinclude"
      body
        = yield
    

    我希望这对某人有所帮助。

    【讨论】:

      猜你喜欢
      • 2011-09-19
      • 2011-12-17
      • 2017-05-07
      • 2012-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-26
      相关资源
      最近更新 更多