哈姆尔模板建造者

发布于 2024-09-15 17:01:16 字数 1391 浏览 5 评论 0原文

我正在将一个旧程序转换为 haml,但我的自定义表单构建器遇到了问题。问题只是在字段标签和输入标签之间添加换行符。

这是原始的 FormBuilder:

# Custom FormBuilder
class SuperFormBuilder < ActionView::Helpers::FormBuilder

  # Create an array of helpers to override with our label builder
  helpers = field_helpers +
            %w{calendar_date_select date_select datetime_select time_select} +
            %w{collection_select select country_select time_zone_select} -
            %w{hidden_field label fields_for} # Don't decorate these

  helpers.each do |name|

    define_method(name) do |field, *args|

      # Get the hash option (EG the field involved)
      options = args.extract_options!

      # Create a label for that field
      label = label(field, options[:label], :class => options[:label_class])

      # Create an inline error
      error = error_message_on(field)

      # Wrap label in paragraph, include the original helper EG: text-field
      @template.content_tag(:p, label + "<br />" + super + error)  #wrap with a paragraph
    end
  end

  # Override the submit button
  def submit
    # Calculate custom text for submit button.
    prefix = object.new_record? ? "Create" : "Update"

    # Wrap submit button in paragraph tags and add custom text.
    @template.content_tag(:p,super(prefix, :id => "apply" ))
  end
end

如果我使用 %br ,其中
;那根本不起作用。有没有办法把它改成 haml 呢?

I'm converting an old program over to haml and I've got a problem with my custom formbuilder. The problem is simply adding a line feed between a fields label and input tag.

Here is the original FormBuilder:

# Custom FormBuilder
class SuperFormBuilder < ActionView::Helpers::FormBuilder

  # Create an array of helpers to override with our label builder
  helpers = field_helpers +
            %w{calendar_date_select date_select datetime_select time_select} +
            %w{collection_select select country_select time_zone_select} -
            %w{hidden_field label fields_for} # Don't decorate these

  helpers.each do |name|

    define_method(name) do |field, *args|

      # Get the hash option (EG the field involved)
      options = args.extract_options!

      # Create a label for that field
      label = label(field, options[:label], :class => options[:label_class])

      # Create an inline error
      error = error_message_on(field)

      # Wrap label in paragraph, include the original helper EG: text-field
      @template.content_tag(:p, label + "<br />" + super + error)  #wrap with a paragraph
    end
  end

  # Override the submit button
  def submit
    # Calculate custom text for submit button.
    prefix = object.new_record? ? "Create" : "Update"

    # Wrap submit button in paragraph tags and add custom text.
    @template.content_tag(:p,super(prefix, :id => "apply" ))
  end
end

If I use a %br where that <br /> is that doesn't work at all. Is there a way to change this over to haml nicely?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

回忆追雨的时光 2024-09-22 17:01:16

如果您只使用 tag("br") 会发生什么?

What happens if you just use tag("br")?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文