无法捕获Rails helper中的块输出

Kit*_*tto 5 ruby ruby-on-rails slim-lang

我遇到了自定义Rails FormBuilder的问题,这让我从昨天晚上开始疯狂.基本上我想为我的一个构建器方法设置一个可选块,以便我可以在main中显示其他内容content_tag:

def form_field(method, &block)
  content_tag(:div, class: 'field') do
    concat label(method, "Label #{method}")
    concat text_field(method)
    capture(&block) if block_given?
  end
end
Run Code Online (Sandbox Code Playgroud)

当我在我的一个Slim模板中调用该方法时,如下所示:

= f.form_field :email do
  small.help-text
    | Your email will never be public.
Run Code Online (Sandbox Code Playgroud)

它在<small>以下实际输出上方插入块(此处为:帮助文本)content_tag:

<small class="help-text">Your email will never be public.</small>
<div class="field">
    <label for="user_email">Label email</label>
    <input type="text" value="" name="user[email]" id="user_email">
</div>
Run Code Online (Sandbox Code Playgroud)

我尝试了其他几个变种,但似乎我永远无法捕捉到的输出block.任何想法 - 甚至可能更有趣:对这种行为的解释?我阅读了几篇关于该主题的文章,并且还看了一下Rails源代码,但无法弄清楚为什么它会像那样表现.

seb*_*ter 5

作为@Kitto说:capture,:concat和许多其他佣工实施@Template.

在我的习俗中FormBuilder,我有这个:

module CustomFormBuilder < ActionView::Helpers::FormBuilder
  delegate :content_tag, :concat, to: :@template

  [ ... your code ... ]
end
Run Code Online (Sandbox Code Playgroud)