将非输入行插入Formtasic表单

Ran*_*ess 15 formtastic ruby-on-rails-3 activeadmin

我在Rails 3.2(使用Active Admin)中使用Formtastic 2.1.1,我想在我的表单中插入一行,但没有输入字段.这是可能的吗?Formtastic DSL中的语法是什么来实现这一目标?

这是一个例子:

form do |f|

    f.inputs "Model Info" do
      f.input :title
      f.input :published
      f.input :path
    end

end
Run Code Online (Sandbox Code Playgroud)

我想做这样的事情:

form do |f|

    f.inputs "Model Info" do
      f.input :title
      f.input :published
      f.input :path
      f.div "This is some important text that people using this form need to know"
    end

end
Run Code Online (Sandbox Code Playgroud)

有没有人以前用Formtastic做过这件事?

Ran*_*ess 23

我自己搞清楚了.我只需要插入html而不需要任何方法调用,如下所示:

form do |f|

  f.inputs "Model Info" do
    f.input :title
    f.input :published
    f.input :path
  end

  f.inputs "Custom HTML Stuff" do
    "<div id=\"element-name\">
      Some kind of content
    </div>".html_safe
  end

end
Run Code Online (Sandbox Code Playgroud)


Ars*_*en7 16

要在任何地方插入任何自定义代码,您可以使用f.form_buffers.last:

form do |f|
  f.form_buffers.last << "<p>Hello world!</p>".html_safe # The simple way

  ft = f.template # just a helper variable

  f.inputs "Foo" do
    f.input :title

    f.form_buffers.last << ft.content_tag(:li) do
      ft.content_tag(:p, "Hello again!") +
          ft.tag(:input, type: :hidden, name: "bar[]", value: "baz")
    end
    f.input :path
  end
end
Run Code Online (Sandbox Code Playgroud)

请注意HTML结构.如果从f.inputs块中调用它,则代码将放在<ol>元素中.在"表单"级别,您在一个<form>元素内.

一点警告:与任何"未记录的功能"一样,此方法可能会在任何新版本中发生更改而不会发出警告.

  • `form_buffers`在最新的Active Admin版本中使用的Formtastic版本已弃用. (5认同)
  • [此评论](https://github.com/activeadmin/activeadmin/issues/3834#issuecomment-93678418)对相关的github问题描述了该弃用的解决方案. (2认同)