Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActiveAdmin customizing the form for belongs_to

I have these associations:

class Course < ActiveRecord::Base
  has_many :signup
  has_many :user, :through => :signup

  accepts_nested_attributes_for :signup
end

class User < ActiveRecord::Base
  has_many :signup
  has_many :course, :through => :signup

  accepts_nested_attributes_for :signup
end

class Signup < ActiveRecord::Base
  belongs_to :course
  belongs_to :user
end

Now, I would like to customize the ActiveAdmin form for "Signup", so it shows the title of the courses and the name of the users as a select and not as a textfield.

The default form already does that, however I need to customize the form further and I can't reproduce the default form.

like image 921
Luís Ramalho Avatar asked Aug 16 '12 23:08

Luís Ramalho


1 Answers

Your form block will look something like this in your admin/signups.rb:

form do |f|
    f.input :course
    t.input :user
end

By default, since course and user are associations, this should give you a collection_select - that is, a with the name attribute of your models as labels, ids as values. If you had passed your inputs a input type, this will force them to display as that type.

form do |f|
    f.input :course, :as => :string
end

This'll just give you a course_id text input field, where you'll probably just enter the ID for the associated object. To "reproduce the default form", just continue adding inputs for the relevant attributes. You can even wrap them in f.inputs to group them and make things look pretty.

form do |f|
    f.inputs "Basic Details" do
        f.input :course
        f.input :user
    end
end
like image 196
Edd Morgan Avatar answered Oct 17 '22 06:10

Edd Morgan