有没有一种方法可以禁用默认的Formtastic布局?

4
对于特定的情况,我希望将表单呈现为(用于原地编辑)的一部分。在formtastic中是否有一种方法可以禁用由.inputs / .buttons生成的布局?而不是


<fieldset> <ol> <li> 

i would like simply to wrap the fields in the

<td>

有没有内置的方法或任何解决这个问题的方案?
4个回答

2

目前 Formtastic 没有内置的方法来更改标记语言。要么使用 CSS 调整现有的标记钩子,要么放弃使用 Formtastic 并编写自己的方式(就像我们以前做的那样)。


@justin-french最近的Formtastic版本是否仍然缺乏提到的功能? - oldhomemovie
@gmile Formtastic 2(Github上未发布的主分支)已经为通过覆盖代码中的小方法来自定义标记做好了基础工作。意图是允许进行小的调整,但我认为它可以雄心勃勃地用于使用表格而不是列表或div等。你所要求的显然是非常复杂的。 - Justin French
我 @gmile 在我的博客上发布了有关新可定制输入的简短介绍。除此之外,您需要进行代码调试。http://justinfrench.com/notebook/formtastic-2-preview-custom-inputs - Justin French
@JustinFrench 你也可以很快地允许用户指定标记... 使用LI作为默认值,但有另一个参数允许覆盖标记。不确定为什么这很复杂。大约在302行附近的input_helper.rb文件中(options [:wrapper] ||:li) - baash05
@JustinFrench,我是你的忠实粉丝!只是想这么说。同时也想说我同意这个帖子。我有点传统,喜欢Bootstrap,但也希望能拥有两个世界。无论如何,感谢你的出色贡献。 - Trip
显示剩余2条评论

1
在 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集成。


1

-2

I wrapped my call to the formtastic bit (in my haml file) in a string and then subbed out the

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

It's a might bit easier than re-writing the form without formtastic, and it worked perfectly.

Admittedly it's a not an ideal solution. For a one off though... I can live with it.


你可以使用<td>代替<fart>。虽然谁能否认屁比td更有趣呢。 - baash05

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接