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') %>

查看:85
本文介绍了Rails 3 –在视图中<%= yield(:sidebar)||不会调用yield return或callback 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).
Testing with Ruby 1.9.1p378 and Ruby 1.9.2dev (2010-04-05 trunk 27225)

Stuck in a situation, i don't know which part will work well. Suspect yield is the problem, but don't know exactly.

In my Layout Files I use the following technique quite often:

app/views/layouts/application.html.erb:

<%= yield(:sidebar) || render('shared/sidebar') %>


For Example the partial look like:

app/views/shared/_sidebar.html.erb:

<p>Default sidebar Content. Bla Bla</p>


Now it is time for the key part!
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 %>


So you've got the idea. Hopefully. This technique worked well in any Rails 2.x Application.
Now, in Rails 3 (beta2) only the yield Part is working.

|| render('shared/sidebar')

The or side will not process by rails or maybe ruby.

Thanks for input and time!

解决方案

Ryan Bates from railscasts.com shows in Episode #227 - Upgrading to Rails 3 Part 3 a solution with content_for?() (video playback at 2:45 Min)

I think, that's the way we should use it:

content_for?(:sidebar) ? yield(:sidebar) : render("shared/sidebar")

这篇关于Rails 3 –在视图中&lt;%= yield(:sidebar)||不会调用yield return或callback render('shared/sidebar')%&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆