【问题标题】:content_tag is not rendered from ActionView::Helpers::FormBuildercontent_tag 不是从 ActionView::Helpers::FormBuilder 呈现的
【发布时间】:2012-05-21 01:22:55
【问题描述】:

我一直在尝试使用 Rails 中的自定义表单构建器方法。我的目标类似于formtasticf.inputs 方法。到目前为止,这是我的代码:

class Builder < ActionView::Helpers::FormBuilder  

  def fieldset(name)
    @template.content_tag :fieldset, :class => 'inputs' do
      @template.content_tag :div, :class => 'legend' do
        @template.content_tag :span, name
      end
      @template.content_tag :ol do
        yield
      end
    end
  end

  def input_field(name, classes='', *args)
    @template.content_tag :li, class: "input #{classes}" do
      label(name) + text_field(name)
    end
  end

end

那么我可以像这样在 HAML 中构建表单:

  = f.fieldset "General Info" do
    = f.input_field :name
    = f.input_field :slug, 'last'

问题是呈现的 HTML 丢弃了图例 DIV。

<fieldset class="inputs">
  <ol>
    <li class="input ">
      <label for="content_instance_name">Name</label>
      <input id="content_instance_name" name="content_instance[name]" size="30" type="text" value="Home Page">
    </li>
    <li class="input last">
      <label for="content_instance_slug">Slug</label>
      <input id="content_instance_slug" name="content_instance[slug]" size="30" type="text" value="home-page">
    </li>
  </ol>
</fieldset>

好像我错过了什么。
提前致谢。

解决方案

这里是调整后的fieldset方法:

def fieldset(name)
  @template.content_tag :fieldset, :class => 'inputs' do
    legend = @template.content_tag :div, @template.content_tag(:span, name), :class => "legend"
    legend += @template.content_tag :ol do
      yield
    end
  end
end

【问题讨论】:

    标签: ruby-on-rails ruby haml formbuilder


    【解决方案1】:

    仅渲染块的返回值。添加一个+,这样您的两个content_tag 调用都会返回。 Similar question here.

    【讨论】:

      猜你喜欢
      • 2011-01-29
      • 2011-11-08
      • 2020-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多