【发布时间】:2011-10-10 12:26:24
【问题描述】:
我正在尝试通过帮助程序呈现 content_for 块的结果。
我有一个模板 (HAML) 和如下布局:
# app/views/books/show.html.haml
-content_for(:page_header) do
%h1= @book.title
# app/views/application.html.haml
...
=yield(:page_header)
...
这绝对没问题。
我想要做的是在一个助手中进行调用。所以我的目标是:
# app/views/books/show.html.haml
-content_for(:page_header) do
%h1= @book.title
# app/views/application.html.haml
....
=page_header(block)
....
# app/helpers/application.rb
....
def page_header(&block)
# Some view logic
# ...
=yield(:page_header)
end
....
我可以通过调用助手来获得部分结果:
# app/views/application.html.haml
=page_header { yield(:page_header) }
# app/helpers/application.rb
def page_header(&block)
yield
end
但这对我来说很难看。
有什么想法吗?提前致谢。
回答:再次使用 content_for(:page_header) 来渲染它。
【问题讨论】:
标签: ruby-on-rails templates helpers