Phi*_*ner 2 rendering ruby-on-rails partial content-for
由于某些原因,我无法在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"
Run Code Online (Sandbox Code Playgroud)
由于某种原因,以下内容不包括我的部分内容(完全渲染后):
/ # => _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
Run Code Online (Sandbox Code Playgroud)
我看不出为什么这行不通。任何帮助表示赞赏。当直接在我的headinclude-partial 内部渲染部分时,一切正常。
这里的问题是订单。我必须content_for在调用之前定义-block render "layouts/headinclude"。
请注意,如果“ answering” -block content_for(包含render "layouts/hotjar"-part的那个)在模板内(例如show,index或当前使用的任何模板),则此概念将起作用。原因是Rails解析内容的顺序。
模板似乎在布局之前已解析,因此在这种情况下,“询问” content_for块将具有要显示的实际数据。
这是(一种可能的)答案:
/ => 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
Run Code Online (Sandbox Code Playgroud)
希望对您有所帮助。