Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Active Admin create form in register_page

Using ActiveAdmin (0.5.1) I would like to make multiple different forms for creating an object and saving it to my database. I'm trying to do this by using ActiveAdmin.register_page, but I'm running into trouble while trying to create the form. It seems that while using register_page, you do not get the same form method as in a register call. Here's the code:

ActiveAdmin.register_page "New Object" do
  content do
    para "Here you can create new objects!"
    para "This content will be replaced with links to the specialized forms"
  end
end

And the code for one of the forms:

ActiveAdmin.register_page "Type 1" do
  menu :label => "Type 1", :parent => "New Object"

  content do
    panel "Attributes" do
      form do |f|
        f.input :color
        f.input :size
    end
  end
end

However this form will not render in any workable manner. Also f.inputs as well as many other methods you can see in examples (like this) do not work. Is it possible to make fully functional forms using ActiveAdmin#register_page?

like image 728
wesdotcool Avatar asked Mar 18 '13 23:03

wesdotcool


2 Answers

To create forms in a non-standard context (i.e., not a resource register), you need to use formtastic's semantic_form_for with the :url and :builder options specified.

content do
  semantic_form_for MyObject.new, :url => admin_my_objects_url, :builder => ActiveAdmin::FormBuilder do |f|
    f.inputs "My Object" do
      f.input :color
      f.input :size
    end
    f.actions
  end
end

This will give you a panel labeled "My Object" containing the form for your object with a submit button below it.

like image 188
Josh Kovach Avatar answered Oct 17 '22 21:10

Josh Kovach


ActiveAdmin.register_page "Families Placement" do
  menu label: "Populaire Familles"

  page_action :update, method: :post do
    Family.find(params['id']).update_attributes(id_1: params['id_1'], id_2: params['id_2'])
    redirect_to "/"
  end

  content do
    Family.all.order(id: :asc).each do |family|
        form action: "families_placement/update", method: :post do |f|
            columns do
          panel family.name do
            f.input :id, type: :hidden, value:  family.id, name: 'id'
            f.input :id_1, as: :select, collection: collect_posts, value: family.id_1, name: 'id_1'
            f.input :id_2, as: :select, collection: collect_posts, value: family.id_2, name: 'id_2'
            f.input :authenticity_token, type: :hidden, name: :authenticity_token, value: form_authenticity_token
            f.input :submit, type: :submit
          end
        end
      end
    end
  end
end

You have an exemple of one working form in a custom ActiveAdmin page. Is very important to add

f.input :authenticity_token, type: :hidden, name: :authenticity_token, value: form_authenticity_token

This is where i found all informations to do this : https://asafdav2.github.io/2016/adding-forms-to-activeadmin-custom-pages/

like image 31
Js60 Avatar answered Oct 17 '22 20:10

Js60