简体   繁体   中英

Rails content_for not rendering partial

For some reason I can not render a partial inside a content_for block.

Here's the code:

/ => 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"

For some reason the following does not include my partial once fully rendered:

  / # => _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

I do not see a reason why this would not work. Any help is appreciated. When rendering the partial directly inside my headinclude -partial, it's working all fine.

The problem here was with the order. I had to define the content_for -block prior to calling render "layouts/headinclude" .

Note that this concept would've worked if the "answering" content_for -block (the one containing the render "layouts/hotjar" -part) was inside a template (Like show or index or in whatever template you currently are). Reason for that is the order the content gets resolved by Rails.

A template seems to get resolved before the layout, so in this case the "asking" content_for -block would've had actual data to display.

Here's (one possible) an answer:

/ => 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

I hope this helps somebody.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM