【问题标题】:Is there a way to disable default formtastic layout?有没有办法禁用默认的formtastic布局?
【发布时间】:2010-11-01 10:53:01
【问题描述】:

对于一种特定情况,我想将表单呈现为(用于就地编辑)的一部分。有没有一种方法可以禁用 .inputs / .buttons 生成的布局?而是一个

<fieldset> <ol> <li> 

我想简单地将字段包装在

<td>

这个问题有内置方法或任何解决方案吗?

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 webforms formtastic


    【解决方案1】:

    Formtastic 中还没有内置的方法来更改标记。要么使用 CSS 来调整足够的标记钩子,要么放弃 Formtastic 来使用这个表单并按照自己的方式编写代码(就像我们过去一样)。

    【讨论】:

    • 我找到了具有这种功能的formtastic fork,但最后我认为完全没有必要使用它。做一种或两种形式是标准的方式,无论如何也不会造成太大的伤害。 :-)
    • @justin-french 最新版本的 Formtastic 是否仍然缺少提到的功能?
    • @gmile Formtastic 2(Github 上未发布的 master 分支)已经为通过覆盖代码中的小方法来自定义标记做好了基础工作。目的是允许进行小的调整,但我认为它可以雄心勃勃地用于使用表格而不是列表或 div 或其他任何东西。你所要求的显然是非常复杂的。
    • 我 @gmile 我在我的博客上发布了对新的可自定义输入的快速介绍。除此之外,您还必须进行代码潜水。 justinfrench.com/notebook/formtastic-2-preview-custom-inputs
    • 也许最好的办法是删除所有与实际控件无关的格式。没有 LI 或 TD.. 将这些留给我们添加,它可能是一个标志来关闭它。
    【解决方案2】:

    目前尚不支持,但您可以使用分叉的 formtastic 版本: https://github.com/linoj/formtastic

    更多详情请访问: http://www.vaporbase.com/postings/Replaceable_render_engines_for_Formtastic

    在 formtastic 论坛上阅读,它甚至可能有一天会合并到原点。

    【讨论】:

      【解决方案3】:

      在 Rails 中,您可以覆盖定义用于渲染元素的标签的函数:

      config/initializers/formtastic_foundation.rb:

      # change required fields advice tag (abbr -> span)
      Formtastic::FormBuilder.required_string =
      proc { Formtastic::Util.html_safe(%{<span title="#{Formtastic::I18n.t(:required)}">*</span>}) }
      
      module Formtastic
        module Helpers
          # change field wrapper (ol -> div)
          module FieldsetWrapper
            protected
            def field_set_and_list_wrapping(*args, &block) #:nodoc:
              contents = args.last.is_a?(::Hash) ? '' : args.pop.flatten
              html_options = args.extract_options!
      
              if block_given?
                contents = if template.respond_to?(:is_haml?) && template.is_haml?
                template.capture_haml(&block)
                else
                  template.capture(&block)
                end
              end
      
              contents = contents.join if contents.respond_to?(:join)
      
              legend = field_set_legend(html_options)
                fieldset = template.content_tag(:fieldset,
                Formtastic::Util.html_safe(legend) << template.content_tag(:div, Formtastic::Util.html_safe(contents)),
                html_options.except(:builder, :parent, :name)
              )
      
              fieldset
            end
          end
        end
      
        module Inputs
          module Base
            # change input wrapper tag (li.default_clases -> div.large-12.columns inside div.row)
            module Wrapping
              def input_wrapping(&block)
                def super_wrapper_html_options
                  {:class => 'row'}
                end
      
                new_class = [wrapper_html_options[:class], "large-12 columns"].compact.join(" ")
      
                template.content_tag(:div,
                  template.content_tag(:div,
                    [template.capture(&block), error_html, hint_html].join("\n").html_safe,
                    wrapper_html_options.merge(:class => new_class)),
                  super_wrapper_html_options)
              end
            end
          end
        end
      end
      

      我使用此代码将 Formtastic 3 与 Foundation 5.4.5 集成

      【讨论】:

        【解决方案4】:

        我将我对 formtastic 位(在我的 haml 文件中)的调用包装在一个字符串中,然后替换掉

        = "#{f.input ...}".gsub('<li class=', '<fart class=').html_safe #remove the li to align this input with the other text in the table. 
        

        这可能比在没有格式的情况下重写表单要容易一些,而且效果很好。

        诚然,这不是一个理想的解决方案。不过,为了一次...我可以忍受它。

        【讨论】:

        • 你可以用 代替 。尽管放屁比 td 有趣得多,但谁能反驳这个事实。
        猜你喜欢
        • 2022-01-20
        • 1970-01-01
        • 2010-09-24
        • 2015-11-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-11-14
        相关资源
        最近更新 更多