30 December 2012

HABTM and ActiveAdmin Forms

has_and_belongs_to_many (HABTM) and Active Admin Forms

For the last day, i've been trying to get a Many to Many (m2m) relationship to be represented properly in an ActiveAdmin CRUD form.  

ActiveAdmin uses Formtastic to render its forms. So looking for a solution to my problem first meant looking through ActiveAdmins documentation then looking through the Formtastic documentation.



I was rather perplexed at first, because the Formtastic documentation is based on the idea of you putting code into your templates, but the ActiveAdmin documentation is based on the idea of you putting code into your /root/app/admin/<modelname>.rb file.  So the answer is to use basically the same api but put it in your ActiveAdmin model file.

As for HABTM though, I really had to dig through the API to get the answer, as far as I can tell, it is not precisely covered in an example.  So here's the answer of how you do a HABTM in ActiveAdmin:


 form do |f|  
       f.inputs "Framework Associations" do  
            f.input :type, :as => :select  
            f.input :language, :as => :select  
            f.input :license, :as => :select, :input_html => { :multiple => true }  
       end  
       f.inputs "Framework Details" do  
            f.input :name  
            f.input :description, :input_html => { :rows => 10, :cols => 20 }  
            f.input :web_url  
            f.input :source_url  
            f.input :docs_url  
            f.input :api_url  
            f.input :current_version  
       end  
  end  

The above code gives you a two sectioned form with select form elements in the top section and string and text input boxes at the bottom.

The key here is this code:


 f.input :type, :as => :select  
 f.input <model name>, :as => :select  
 f.input :language, :as => :select  
 f.input :license, :as => :select, :input_html => { :multiple => true }  
 f.input <model name>, :as => :select, <options>  

The top two select boxes are single select boxes, they take the name of the model you're getting select options from as an argument to the input method.

With HABTM, even though you've had to do a migration to create the intermediary table, you want to put the name of the opposing model (in this case "License").

So in my Framework.rb model file i have:

 class Framework < ActiveRecord::Base  
  attr_accessible :api_url, :current_version, ...
  has_and_belongs_to_many :license  
 end  

And therefore in my ActiveAdmin / Formtasic code you use the same model name as the argument in to the input method, just remember to add the :input_html option of :multiple => true.

Done!!

2 comments:

  1. Can we use HABTM with accepts_nested_attributes_for in active admin? As i am using this but there is problem in form and the problem is it not generating proper ids for form element. Can you help me?

    ReplyDelete
    Replies
    1. Can we use HABTM with accepts_nested_attributes_for in active admin? As i am using this but there is problem in form and the problem is that it is not generating proper ids for form element. Can you help me?

      Delete