Rails 3 –在视图中<%= yield(:sidebar)||不会调用yield return或callback render('shared/sidebar')%> [英] Rails 3 - yield return or callback won't call in view <%= yield(:sidebar) || render('shared/sidebar') %>
问题描述
我正在将网站从Rails 2(最新)迁移到Rails 3(beta2).
使用 Ruby 1.9.1p378 和 Ruby 1.9.2dev (2010-04-05主干27225)进行测试>
卡在一种情况下,我不知道哪一部分会很好.怀疑是yield
的问题,但不清楚.
在布局文件中,我经常使用以下技术:
app/views/layouts/application.html.erb
:
<%= yield(:sidebar) || render('shared/sidebar') %>
例如,部分外观如下:
app/views/shared/_sidebar.html.erb
:
<p>Default sidebar Content. Bla Bla</p>
现在是关键部分的时候了!
在任何视图中,我都想创建一个content_for
块(可选).这可能包含下面的HTML等示例.如果设置了此块,则内部的pice HTML应该在application.html.erb
中呈现.
如果没有,Rails应该在右侧的shared/_sidebar.html.erb
处渲染Partial.
app/views/books/index.html.erb
:
<% content_for :sidebar do %>
<strong>You have to read REWORK, a book from 37signals!</strong>
<% end %>
所以您有了主意.希望.这项技术在任何Rails 2.x应用程序中都能很好地发挥作用.
现在,在 Rails 3 (beta2)中,只有yield
部分正在工作.
|| render('shared/sidebar')
or
一侧将不会通过滑轨或可能是红宝石进行处理.
感谢您的投入和时间!
railscasts.com中的Ryan Bates在情节 I'm migrating a Website from Rails 2 (latest) to Rails 3 (beta2). Stuck in a situation, i don't know which part will work well. Suspect In my Layout Files I use the following technique quite often:
For Example the partial look like:
Now it is time for the key part!
So you've got the idea. Hopefully. This technique worked well in any Rails 2.x Application. The Thanks for input and time! Ryan Bates from railscasts.com shows in Episode #227 - Upgrading to Rails 3 Part 3 a solution with I think, that's the way we should use it:
这篇关于Rails 3 –在视图中<%= yield(:sidebar)||不会调用yield return或callback render('shared/sidebar')%>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
Testing with Ruby 1.9.1p378 and Ruby 1.9.2dev (2010-04-05 trunk 27225)yield
is the problem, but don't know exactly.app/views/layouts/application.html.erb
:<%= yield(:sidebar) || render('shared/sidebar') %>
app/views/shared/_sidebar.html.erb
:<p>Default sidebar Content. Bla Bla</p>
In any view, I want to create a content_for
block (optional). This can contain a pice of HTML etc. example below. If this block is set, the pice HTML inside should render in application.html.erb
.
If not, Rails should render the Partial at shared/_sidebar.html.erb
on the right hand side.app/views/books/index.html.erb
:<% content_for :sidebar do %>
<strong>You have to read REWORK, a book from 37signals!</strong>
<% end %>
Now, in Rails 3 (beta2) only the yield
Part is working.|| render('shared/sidebar')
or
side will not process by rails or maybe ruby.content_for?()
(video playback at 2:45 Min)content_for?(:sidebar) ? yield(:sidebar) : render("shared/sidebar")