At some point, you may need to create custom forms in Active Admin for admin users to create records. Active Admin uses Formtastic, a FormBuilder DSL, to output these forms.
In this post, I’m going to show you some of the things you can do with them.
The Model
Let’s say we have a user model that requires a first name, email, and user ID with an optional last name.
class User < ActiveRecord::Base
validates :first_name, presence: true
validates :last_name, allow_nil: true
validates :email, presence: true
validates :user_id, presence: true
end
We can add a form that allows an admin to create a user with both first and last names and the email, while we manually create a user ID for them. Our form will validate based on what we tell it in the model.
Creating the Form
Creating the form is as simple as listing which of the fields from the model we want to display as inputs. By default, if we provide "f.inputs" on its own in a form, we will get input fields for every property on the model. Since we don't want the user ID to be provided by the admin user, we can customize which inputs are shown.
ActiveAdmin.register User do
form do |f|
f.inputs do
f.input :first_name
f.input :last_name, required: false
f.input :email
end
f.actions
end
end
By default, our inputs will display as text boxes (because they are text). You can see all of the available input types, including checkboxes, dates, and even file uploaders, listed in the Formtastic documentation.
In this case, we will provide default actions for the form. This will set up a "Create User" button and a "Cancel" button. If we want to customize these, we can do so in a way similar to the inputs.
f.actions do
f.submit 'Create a new user' as: :button
end
Adding the Controller
We can create a controller below the code for the form to override the User controller. This allows us to take the inputs to the form and store, modify, or use them in whatever way we see fit. In the case of our example, we want to create a new User with the provided inputs, and then manually assign a user ID for them. The values from the form can be pulled out of "params."
controller do
attrs = params[:user]
first_name = attrs[:first_name]
last_name = attrs[:last_name]
email = attrs[:email]
user_id = first_name[0] + last_name
@user = User.new(first_name: first_name,
last_name: last_name,
email: email,
user_id: user_id)
if @user.save
redirect_to '/admin/users'
else
render :new
end
end
If a user is successfully created, the admin user will be redirected to the list of users on the admin portal. If not, they won't be able to proceed.
Displaying Validation Errors
If the admin user missed entering a required field, we re-render the form. In this case, we should provide them with some feedback about what went wrong.
Our Active Record model will collect validation errors for us, and we must provide a way to display them. The only thing we need to do is add a line to the form to indicate where to display the errors.
form do |f|
f.inputs do
f.input :first_name
f.input :last_name, required: false
f.input :email
f.semantic_errors *f.object.errors.keys
end
f.actions
end
These errors show up in red when the page is re-rendered upon failed validation.
Other Fun with Active Admin Forms
There are many other ways to customize your Active Admin forms, including providing ways for admin users to upload files. Check out some of the other posts that expand upon Active Admin forms: