I have a layout (application.html) that has this:
我有一个布局(application.html):
<!-- then anything added in the views -->
<%= yield :stylesheet %>
If I put this code in a 'root' template (ie the one called by an action directly) it renders on the page no problem:
如果我把这段代码放在“根”模板中(即直接被操作调用的那个模板),它在页面上呈现的结果没有问题:
<% content_for :stylesheet do %>
This is a test
<% end %>
But if I put the same thing in a partial it doesn't get rendered. The partial is rendered as such:
但是如果我把同样的东西放到偏微分中它就不会被渲染。部分内容如下:
<%= render :partial => "detail" %>
2 个解决方案
#1
4
Without seeing rest of the related code it is hard to know for sure but it could be an ordering problem.
如果没有看到其他相关代码,就很难确切地知道,但这可能是一个排序问题。
From a high level, when the page renders, it first loads the view (before the layout) and any partials in the view with content_for should work correctly. However if this partial is being rendered in the layout instead of the view template, there is a chance the content_for will be called after the "yield :stylesheet" which would cause it to get ignored.
从高层来说,当页面呈现时,它首先加载视图(在布局之前),并且视图中带有content_for的任何部分都应该正确工作。但是,如果这个部分在布局中呈现,而不是在视图模板中呈现,那么可能会在“yield:stylesheet”之后调用content_for,这会导致它被忽略。
#2
0
hmm, are you sure the partial is in the right directory.
嗯,你确定分部在正确的目录里吗?
I'm doing the exact same thing and it works no problem. in my version of home/_detail.html.erb
(i.e., it's in the home directory) in my index.html.erb
page i just put the same code as you have and it works no problem. You sure the spelling is write on the file name, and that you saved the file?
我做的是同样的事情,没有问题。在我的home/_detail.html版本中。erb(即。在我的index.html中。erb页面,我只是放了和你一样的代码,它没有问题。你确定这个拼写是写在文件名上的,并且你保存了这个文件?
Are you sure you're spelling content for correctly or don't have misplaced tags? What do your tests say?
你确定你拼写的内容是正确的还是没有放错的标签?你的测试说明了什么?
Also if it is in the head section, what if you added comment tags, instead of just plain text, maybe it doesn't like that?
如果它在标题部分,如果你添加了注释标签,而不是纯文本,它可能不喜欢那样?
#1
4
Without seeing rest of the related code it is hard to know for sure but it could be an ordering problem.
如果没有看到其他相关代码,就很难确切地知道,但这可能是一个排序问题。
From a high level, when the page renders, it first loads the view (before the layout) and any partials in the view with content_for should work correctly. However if this partial is being rendered in the layout instead of the view template, there is a chance the content_for will be called after the "yield :stylesheet" which would cause it to get ignored.
从高层来说,当页面呈现时,它首先加载视图(在布局之前),并且视图中带有content_for的任何部分都应该正确工作。但是,如果这个部分在布局中呈现,而不是在视图模板中呈现,那么可能会在“yield:stylesheet”之后调用content_for,这会导致它被忽略。
#2
0
hmm, are you sure the partial is in the right directory.
嗯,你确定分部在正确的目录里吗?
I'm doing the exact same thing and it works no problem. in my version of home/_detail.html.erb
(i.e., it's in the home directory) in my index.html.erb
page i just put the same code as you have and it works no problem. You sure the spelling is write on the file name, and that you saved the file?
我做的是同样的事情,没有问题。在我的home/_detail.html版本中。erb(即。在我的index.html中。erb页面,我只是放了和你一样的代码,它没有问题。你确定这个拼写是写在文件名上的,并且你保存了这个文件?
Are you sure you're spelling content for correctly or don't have misplaced tags? What do your tests say?
你确定你拼写的内容是正确的还是没有放错的标签?你的测试说明了什么?
Also if it is in the head section, what if you added comment tags, instead of just plain text, maybe it doesn't like that?
如果它在标题部分,如果你添加了注释标签,而不是纯文本,它可能不喜欢那样?