rails自定义帮助程序不连接div

时间:2014-08-14 22:11:28

标签: ruby-on-rails html-helper helpers

我有以下帮助方法:

def get_divs_for(attr)
    content_tag :div, class: 'form-group' do
      label_tag attr, class: 'col-sm-2 control-label' +
      content_tag(:div, class: 'col-sm-10') do
        text_field_tag(attr,id: attr, :class => 'form-control') +
        content_tag(:span, attr, id: "message-" + attr, :class => "confirmMessage")
      end
    end
end

但是,不是呈现完整内容:

<div class="form-group">
    <label class="col-sm-2 control-label" for="name">Name</label>
    <div class="col-sm-10">
       <input class="form-control" id="name" name="company[name]" type="text">
       <span id="message-name" class="confirmMessage">Name</span>
     </div>
</div>

我只能得到这个:

<div class="form-group">
    <span class="confirmMessage" id="messageName">Name</span>
</div>

我尝试使用连接而不是+但得到了相同的结果。

2 个答案:

答案 0 :(得分:1)

def get_divs_for(attr)
    content_tag :div, class: 'form-group' do
      label_tag(attr, class: 'col-sm-2 control-label') +
      content_tag(:div, class: 'col-sm-10') do
        text_field_tag(attr,id: attr, :class => 'form-control') +
        content_tag(:span, attr, id: "message-" + attr, :class => "confirmMessage")
      end
    end
end

它试图与您的标签类连接而不是标签html标签

答案 1 :(得分:1)

这样做了:

def get_divs_for(attr)
content_tag(:div, class: "form-group") do
  label_tag(attr, attr, class: "col-sm-2 control-label") +
  content_tag(:div, class: "col-sm-10") do
    text_field_tag(attr, nil, class: "form-control") +
    content_tag(:span, nil, id: "message-" + attr, class: "confirmMessage")
  end
end

显然它不是很满意:class =&gt; 'className和首选类:'className'

谢谢你的帮助