how to embed raw html in active_admin formtastic

2019-01-25 19:58发布

I'm trying to build a form, with formtastic, inside an active_admin model. The problem is I need a certain script tag and other raw HTML stuff directly inside or around the form.

I'm doing it with the normal form block:

form do |f|
    f.inputs :name => "User Details", :for => :user do |user_form|
    user_form.input :first_name, :required => true
    ...

How do I embed a simple div tag right in between? Or even a script tag?

I thought about using a render :partial, but I want to know if the above method is possible first. Thanks!

3条回答
时光不老,我们不散
2楼-- · 2019-01-25 20:15

You can insert a div or javascript like this:

   f.form_buffers.last << content_tag(:div, "Div content here")
   f.form_buffers.last << javascript_tag("alert('hi');")
查看更多
The star\"
3楼-- · 2019-01-25 20:24

In the current ActiveAdmin the form_buffers is deprecated. Instead it's possible to do the following:

insert_tag(Arbre::HTML::Div) { content_tag(:span, "foo") }
查看更多
Anthone
4楼-- · 2019-01-25 20:38

Active admin created a DSL on top of Formtastic according to their docs

https://github.com/activeadmin/activeadmin/blob/master/docs/5-forms.md

So you can now do:

form do |f|
  f.semantic_errors(*f.object.errors.keys)

  import_errors = self.controller.instance_variable_get("@errors")
  if import_errors.present?
    ul class: 'errors' do
      import_errors.each do |e|
        li e
      end
    end
  end
 # ...
end
查看更多
登录 后发表回答