Yes, it is possible to create a custom form in Active Admin without using an ActiveRecord database model. Active Admin is built on top of the Rails framework, and while it is primarily designed to work with ActiveRecord models, you can still create custom forms and interact with non-ActiveRecord models or even external APIs.
Here’s how you can achieve this:
-
Define a Plain Ruby Class (Model): Create a plain Ruby class that represents your custom model. This class does not need to inherit from
ActiveRecord::Base
. You can useActiveModel::Model
to include basic model behavior like validations and form handling.# app/models/custom_model.rb class CustomModel include ActiveModel::Model attr_accessor :name, :email, :message validates :name, presence: true validates :email, presence: true, format: { with: URI::MailTo::EMAIL_REGEXP } end
-
Create a Custom Form in Active Admin: In your Active Admin resource file, define a custom form using the
form
block. You can use theform_for
method to create a form for your custom model.# app/admin/custom_forms.rb ActiveAdmin.register_page "Custom Form" do content do render partial: 'admin/custom_forms/form' end page_action :submit, method: :post do @custom_model = CustomModel.new(custom_model_params) if @custom_model.valid? # Handle the valid data (e.g., send an email, call an API, etc.) redirect_to admin_custom_form_path, notice: "Form submitted successfully!" else render :form end end controller do def custom_model_params params.require(:custom_model).permit(:name, :email, :message) end end end
-
Create a Partial for the Form: Create a partial view for the form. This will be rendered in the Active Admin page.
# app/views/admin/custom_forms/_form.html.erb <%= form_for @custom_model || CustomModel.new, url: admin_custom_form_submit_path, method: :post do |f| %> <% if @custom_model&.errors&.any? %> <div id="error_explanation"> <h2><%= pluralize(@custom_model.errors.count, "error") %> prohibited this form from being saved:</h2> <ul> <% @custom_model.errors.full_messages.each do |message| %> <li><%= message %></li> <% end %> </ul> </div> <% end %> <div class="field"> <%= f.label :name %> <%= f.text_field :name %> </div> <div class="field"> <%= f.label :email %> <%= f.email_field :email %> </div> <div class="field"> <%= f.label :message %> <%= f.text_area :message %> </div> <div class="actions"> <%= f.submit "Submit" %> </div> <% end %>
-
Add a Route for the Custom Form: Add a route for the custom form and its submission action in your
config/routes.rb
file.# config/routes.rb namespace :admin do get 'custom_form', to: 'custom_forms#index' post 'custom_form/submit', to: 'custom_forms#submit', as: 'custom_form_submit' end
-
Handle Form Submission: In the
page_action :submit
block, you can handle the form submission. For example, you might send an email, call an external API, or process the data in some other way.
- No Database Required: Since you’re using a plain Ruby class, there’s no need for a database table.
- Validations: You can still use ActiveModel validations to ensure data integrity.
- Flexibility: You can use this approach to interact with external APIs or services instead of a database.
This approach allows you to leverage Active Admin’s interface while working with non-ActiveRecord models or custom logic.