Skip to content

Form Partial Tricks

Nguyễn Đức Long edited this page Apr 14, 2020 · 7 revisions

Submit & Cancel Buttons

In order to have Active Admin-style submit & cancel buttons, a specific approach is neccessary because the usual f.actions is a special function defined in Active Admin Arbre component, hence, is not accessible outside form block or active_admin_form_for block.

Outside Arbre, if you want to have a consistent look on your forms, you can easily append the cancel link to the Formtastics actions using #cancel_link provided by ActiveAdmin::FormBuilder:

<%= semantic_form_for resource, builder: ActiveAdmin::FormBuilder do |f| %>
  <%= f.actions do %>
    <%= f.action :submit %>
    <%= f.cancel_link # Default to index page %>
    <%= f.cancel_link action: :show # Link to resource show page %>
    <%= f.cancel_link other_route_path %>
    <%= f.cancel_link '/other/path' %>
  <% end %>
<% end %>

Datepicker

If, for some reason or another, you had to use a form partial, and you want to add a datepicker, you have to go about it a little differently, as :as => :datepicker won't work. Instead, do this (HAML style):

= semantic_form_for [:admin, @user] do |f|
  = f.inputs "General" do
    = f.input :birth_date, :as => :string, :input_html => {:class => "datepicker"}

Use the default ActiveAdmin::FormBuilder

You can access of the standard active_admin form builder addons by give semantic_form_for a builder arg like this:

= semantic_form_for [:admin, @user], builder: ActiveAdmin::FormBuilder do |f|
  = f.inputs "General" do
     f.input :birth_date, :as => :string, :input_html => {:class => "datepicker"}

It's a little tricky because they don't buffer output the same, you usually only need a <%= for the last top-level wrapper.

Render partials within form block

For when you want to render only part of a form from a partial:

# In app/admin/foos.rb
render partial: 'bar_inputs', locals: { f: f }

Create a .html.arb file at app/views/active_admin/resource/_bar_inputs.html.arb with the contents you want in the partial, e.g.

f.inputs 'Some fields' do
  f.input :name
  f.input :title
  f.input :description
end
Clone this wiki locally