问题描述
我正在Rails 3.2(使用Active Admin)中使用Formtastic 2.1.1,并且我想在我的表单中插入不没有输入字段的行。这可能吗?在Formtastic DSL中使用什么语法来实现这一目标?
I am using Formtastic 2.1.1 in Rails 3.2 (with Active Admin) and I want to insert a row into my form that does not have an input field present. Is this possible and what is the syntax in the Formtastic DSL to achieve this?
以下是示例:
form do |f|
f.inputs "Model Info" do
f.input :title
f.input :published
f.input :path
end
end
我想做这样的事情:
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
以前有人用过Formtastic吗?
Has anyone done this with Formtastic before?
推荐答案
要在中插入任何自定义代码任何位置,您可以使用 f.form_buffers.last
:
To insert any custom code in any place, you may use 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
请注意HTML结构。如果从 f.inputs
块调用此代码,则代码将放置在< ol>
元素内。在表单级别上,您位于< form>
元素内。
Just be careful about the HTML structure. If you call this from f.inputs
block, your code will be placed inside an <ol>
element. On the "form" level, you are inside a <form>
element.
有点警告:与任何未记录的功能一样,此方法在任何新版本中都可能更改而不会发出警告。
A little warning: As with any "undocumented feature" this method may change without warning in any new release.
这篇关于将非输入行插入到Formtasic表单中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!