gpt4 book ai didi

ruby-on-rails - 如何在 Rails 助手中将内容包装在 html 中

转载 作者:数据小太阳 更新时间:2023-10-29 06:48:36 25 4
gpt4 key购买 nike

我想在 Rails 3 助手中用 HTML 包装一些内容,这样在我看来我可以这样做:

<%= rounded_box do-%>
<%= raw target.text %>
<% end -%>

我有一个如下所示的辅助方法:

def rounded_box(&block)
str = "<div class='rounded_box'><div class='rounded_box_content'><div class='rounded_box_top'></div>
str << yield
str << "<div class='rounded_box_bottom'><div></div></div></div>"
raw str
end

我现在使用它的方式返回正确包装在 HTML 字符串中的内容,但不会在呈现 rounded_box block 中的任何 erb 之前返回(例如,在这种情况下,target.text 呈现两次,一次包装,一次不包装)。

有更好的方法吗?为简单起见,我想避免使用 content_tag,但如果这是我能做到的唯一/最佳方式。

最佳答案

在 block 上调用 capture 而不是 yield:

def rounded_box(&block)
str = "<div class='rounded_box'><div class='rounded_box_content'><div class='rounded_box_top'></div>"
str << capture(&block)
str << "<div class='rounded_box_bottom'><div></div></div></div>"
raw str
end

关于ruby-on-rails - 如何在 Rails 助手中将内容包装在 html 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5254252/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com