热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

content_for不在部分视图中呈现-content_forarenotrenderinginpartialviews

Ihavearailsappwhichusesalayout我有一个使用布局的rails应用程序Thesimplifiedversionlookslikethis:简

I have a rails app which uses a layout

我有一个使用布局的rails应用程序

The simplified version looks like this:

简化版本如下所示:


  
    <%= render 'layouts/head' # renders the "layouts/_head.html.erb" partial correctly  
                              # the most head (css/js) content gets implemented here %>
  
  

    <%= yield # renders the current action %>

    
    <%= content_for :scripts_bottom %>
  

in my `layouts/_head.html.erb' I use

在我的'layouts / _head.html.erb'我用

 <%= content_for :scripts_head %>
 

In my partials I place the following snippets to append them :scripts_head. (some of my partials should put Javascripts

在我的部分内容中,我将以下片段附加到它们:scripts_head。 (我的部分内容应该放在Javascripts中

<% content_for :scripts_head do %>
    <%= Javascript_include_tag 'some_script' %>
<% end %>

The content_for in the `layouts/head' renders nothing

`layouts / head'中的content_for没有任何内容

How can I resolve that?

我怎么解决这个问题?

It looks like that partials are not able to append their content_for content when the content_for :blah do is placed BEHIND the echoing content_for / yield tag.

当content_for:blah do放置在echoing content_for / yield标记之后,看起来部分无法附加其content_for内容。

If I try try content_for :scripts_bottom it will get rendered at the bottom of the page.

如果我尝试尝试content_for:scripts_bottom,它将在页面底部呈现。

Thanks in advance

提前致谢

Rails 3.2.14 ruby 2.0.0p247

Rails 3.2.14 ruby​​ 2.0.0p247

3 个解决方案

#1


0  

instead of provide, try <%= content_for :scripts_head %>

而不是提供,请尝试<%= content_for:scripts_head%>

#2


0  

If you want to use content_for then you need to yield it in your head instead of render.

如果你想使用content_for,那么你需要在头脑而不是渲染中产生它。

So your header would look like:

所以你的标题看起来像:

<%= yield :scripts_head %>

<%= yield:scripts_head%>

Alternatively you can remove the content_for in your partial and just have the JS by itself like this:

或者,您可以删除部分中的content_for,并且只需要像这样拥有JS:

<%= Javascript_include_tag 'some_script' %>

<%= Javascript_include_tag'some_script'%>

Then you wouldn't have to change your layout file.

然后,您不必更改布局文件。

#3


0  

In your layouts/head partial, use yield :scripts_head not content_for

在layouts / head partial中,使用yield:scripts_head而不是content_for


推荐阅读
author-avatar
别禳莴觞芯_737
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有