47

I'm using Active Admin to provide an admin to some models. I need to provide a customized new form for one of them, but leave the edit form as the default provided by Active Admin. Here's what I have. It works in that it is giving me the new form I want, but the edit form is also using the new form, which is not what I want:

ActiveAdmin.register Document do
  form :partial => 'form'
end

I've tried this, but it gives an error that 'new' is an undefined method:

ActiveAdmin.register Document do
  new do
    form :partial => 'form'
  end
end
Andrew Grimm
  • 78,473
  • 57
  • 200
  • 338
Michael Irwin
  • 3,119
  • 5
  • 24
  • 40

5 Answers5

101

If you just want to hide or show certain fields on the new form (e.g. a field that you generate automatically in the model using before_create), you can do this:

form do |f|
    f.inputs "Member Details" do
        f.input :first_name
        f.input :last_name
        f.input :email
        if !f.object.new_record?
            f.input :password
            f.input :password_confirmation
        end
    end
    f.button :Submit
end

This will hide the password fields when you create a new member in the event that you automatically generate passwords the first time the member is created.

Community
  • 1
  • 1
ryanb006
  • 1,011
  • 2
  • 7
  • 2
  • 1
    For some reason if I try this, my new form is fine, but my edit form now has no fields. Any suggestions? – Martin Foot Jun 28 '12 at 18:32
  • It seems to work for me if you specify the if and else clause of ...new_record? : `if f.object.new_record? f.input :email else f.input :email f.input :password f.input :password_confirmation end` – Jan Sep 06 '12 at 08:35
  • Don't forget to include 'f.buttons :commit' in order to have a form submit button. – chech Jun 05 '13 at 19:11
  • @hlegius what does "DAT POG" mean? – Andrew Grimm Jun 23 '17 at 04:16
  • 1
    Just a minor improvement to the code: Instead of `if !f.object.new_record?` one could write `if f.object.persisted?`. Removes the negation and makes it easier to read :) – snrlx Aug 17 '18 at 11:50
10

I've figured out a way to do it with some logic in the view. Not the best way, to be sure, but it does what I want until I figure out a better way. Here's the logic I'm using:

<% if controller.action_name == 'new' %>
new form
<% else %>
edit form
<% end -%>
Michael Irwin
  • 3,119
  • 5
  • 24
  • 40
  • 1
    there has got to be a better way to do this and I would love to know what it is! this is plaguing me at the moment. – sorens Aug 28 '11 at 06:15
  • 1
    @sorens - not in the current version of Active Admin. You will have to separate the forms in the view logic, ActiveAdmin doesn't distinguish between edit and new form. – eugen Sep 01 '11 at 22:52
  • Alternatively you can check if the object is a new or existing record: f.object.new_record? – digitalWestie Feb 07 '23 at 12:43
2

I am not sure it can be done directly with form. If you take a look at the code you'll see that only the last call is taken into account. On the other hand, you may try something like:

config.set_page_config :new do
  form :partial => 'form'
end

But I would rather ask the developers for this feature.

Serabe
  • 3,834
  • 19
  • 24
1

If someone wants to render different partials for new and edit pages you have to:

#app/admin/document.rb
ActiveAdmin.register Document do
  form partial: 'form'
end

#app/views/admin/documents/_form.html.erb
<% if @document.new_record? %>
  <%= render partial: "form_new", resource: @document %>
<% else %>
  <%= render partial: "form_edit", resource: @document %>
<% end %>

#app/views/admin/documents/_form_new.html.erb
<%= semantic_form_for [:admin, @document], builder: Formtastic::FormBuilder do |f| %>
  <%= f.semantic_errors %>
  <%= f.inputs do %>
    <%= f.input :name %>
  <% end %>
  <%= f.actions %>
<% end %>
across
  • 537
  • 8
  • 16
0

You could create a custom page that acts as the new form, render a partial there which contains arbitrary form code.

So, in your admin directory you make a file new_document.rb containing

  ActiveAdmin.register_page "New Document" do

    content do
      panel "Create a new document" do
        render :partial => "admin/documents/custom_form", :locals => {document: Document.new}
      end 
    end

  end

You then put your arbitrary formtastic form in admin/documents/custom_form and your arbitrary controller action aka collection_action in admin/documents.

So basically doing normal rails type stuff within the activeadmin framework.

mattfitzgerald
  • 353
  • 2
  • 14