简体   繁体   English

在视图帮助器中给定的块后呈现内容

[英]Render content after given block in view helper

I have a view helper method that takes a block. 我有一个需要块的视图帮助器方法。 Render that block is no problem, but when I try to add content after the block is rendered, it does not work. 渲染该块没有问题,但是当我尝试在渲染该块之后添加内容时,它将无法工作。

  def validation_div(&block)
    content_tag :div do
      yield
      content_tag :div do
        'This content is never rendered!'
      end
    end
  end

The above code just yields the block and skips the other content. 上面的代码仅产生该块,并跳过其他内容。 I have also tried with: content_for , capture , concat and with_output_buffer without any success. 我也尝试过: content_forcaptureconcatwith_output_buffer没有成功。 As you notice, I don't really understand how these methods work... But my question is: how to render something after the given block is rendered. 如您所见,我不太了解这些方法的工作原理。但是我的问题是:如何在渲染给定的块之后渲染某些东西。 Thanks! 谢谢!

If you look at a content_tag definition, you see it only outputs one tag, one class, and one content. 如果查看content_tag定义,则会看到它仅输出一个标签,一个类和一个内容。 You're trying to get it out out multiple contents so you'll need to join them. 您正在尝试从多个内容中获取内容,因此您需要将它们加入。

def validation_div(&block)
  content = capture(&block)
  content_tag :div do
    content + content_tag(:div , "This content is never rendered!")
  end
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM