由于某种原因,我无法在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内部渲染部分时,一切正常。 最佳答案
这里的问题是订单。
我必须在调用content_for
之前定义render "layouts/headinclude"
-block。
请注意,如果“answering” content_for
-block(包含render "layouts/hotjar"
-part的那个)位于模板(例如show
或index
或当前使用的任何模板)内,则此概念将起作用。原因是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
希望对您有所帮助。
关于ruby-on-rails - Rails content_for不局部渲染,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41699671/