45
votes

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
5

5 Answers

96
votes

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.

10
votes

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 -%>
2
votes

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.

1
votes

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 %>
0
votes

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.