我的一个助手中有以下代码:
def send_fragments_to_other_template(template, counter)
if counter == 0
content_tag "div", class: "card w-5c" do
content_tag "div", class: "card__content resource" do
content_tag "h4" do
content_tag "span", "Send fragments to other template"
end
link_to "Send", edit_admin_template_fragment_path(template), class: "btn btn--s-purple", remote: true
end
end
end
end
问题出在这里:无论出于何种原因,第三个和第四个内容标签都被rails忽略。我在页面上有两个div和按钮(锚),但没有h4或span。
我在做什么错?
它看起来类似于这篇关于堆栈溢出的文章:Why does this nested content_tag not render properly?这里发生的是content_tag仅呈现do-end块中的最后一个表达式。这可能就是为什么您可以看到“发送”按钮而不是其他按钮的原因。我更喜欢使用行为类似于puts的concat来解决这个问题。这是上面帖子中的第二个答案^。
def send_fragments_to_other_template(template, counter)
if counter == 0
content_tag "div", class: "card w-5c" do
content_tag "div", class: "card__content resource" do
content_tag "h4", class: nil do
content_tag "span", "Send fragments to other template", class: nil
end
end
end
end
end
您将在下面得到这样的结果
<div class="card__content resource"> <h4> <span>Send fragments to other template</span> </h4> </div>
我刚刚删除了link_to以测试输出,但是稍后您可以添加它