我已经远离了Rails一段时间,所以也许我错过了一些简单的东西.
你怎么能完成这个:
<%= yield_or :sidebar do %> some default content <% end %>
甚至:
<%= yield_or_render :sidebar,'path/to/default/sidebar' %>
在第一种情况下,我正在尝试:
def yield_or(content,&block) content_for?(content) ? yield(content) : yield end
在第二种情况下:
def yield_or_render(content,template) content_for?(content) ? yield(content) : render(template) end
当没有定义内容时,这可以工作,但是一旦使用content_for来覆盖默认内容,它会引发相同的错误.
我使用this作为起点,但它似乎只在视图中直接使用时才起作用.
谢谢!
解决方法
这样的东西怎么样?
<% if content_for?(:whatever) %> <div><%= yield(:whatever) %></div> <% else %> <div>default_content_here</div> <% end %>
原文地址:https://www.jb51.cc/ruby/271002.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。