为什么'收益'收益率是两倍?

时间:2011-09-01 10:26:23

标签: ruby-on-rails

我有以下代码:

def bootstrap_form_field(f, method, text, &block)
  content_tag :div, :class => div_classes(f, method) do
    f.label(method.to_sym, text) +
    content_tag(:div, :class => "input") do
      concat(yield + error_display(f.object, method))
    end
  end
end

我期待产生这样的东西:

<div class="clearfix error">
  <label for="xlInput">X-Large Input</label>
  <div class="input">
    <input class="xlarge error" id="xlInput" name="xlInput" size="30" type="text">
    <span class="help-inline">Small snippet of help text</span>
  </div>
</div>

来自以下的电话:

<%= bootstrap_form_field f, :password, "Password" do %>
  <%= f.password_field :password %>
<% end -%>

但是我的收益似乎是两次收益:

<div class="clearfix error">
  <label for="user_password">Password</label>
  <div class="input">
    <input id="user_password" name="user[password]" size="30" type="password">
    <input id="user_password" name="user[password]" size="30" type="password">
    <span class="help-inline">can't be blank</span>
  </div>
</div>

怎么样?

2 个答案:

答案 0 :(得分:1)

<%= bootstrap_form_field f, :password, "Password" do %>
  <%= f.password_field :password %>
<% end -%>

=&GT;

<% bootstrap_form_field f, :password, "Password" do %>
  <%= f.password_field :password %>
<% end -%>

答案 1 :(得分:0)

  def bootstrap_form_field(f, method, text, &block)
    content_tag :div do
      f.label(method.to_sym, text) +
        content_tag(:div, :class => "input") do
          yield + error_display(f.object, method)
        end
    end
  end

应该有效。没有连续但我不记得为什么。

编辑:

参见https://github.com/rails/rails/blob/4d3ec4c6a8584fdd9275576ecab07302973e7cc5/actionpack/lib/action_view/helpers/text_helper.rb#L51我认为concat是在helper中向输出缓冲区添加字符串的正确方法。因此,使用

两次获取文本是正常的
concat(yield)