Skip to content

Instantly share code, notes, and snippets.

@eileencodes
Created August 2, 2012 19:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eileencodes/3240041 to your computer and use it in GitHub Desktop.
Save eileencodes/3240041 to your computer and use it in GitHub Desktop.
adding category selectors to active admin
index do
column :title, :sortable => :title do |post|
link_to post.title, [:edit_admin, post]
end
column :categories do |post|
table_for post.categories.order('title ASC') do
column do |category|
link_to category.title, [ :admin, category ]
end
end
end
default_actions
end
show do
attributes_table do
row :title
row :content
table_for post.categories.order('title ASC') do
column "Categories" do |category|
link_to category.title, [ :admin, category ]
end
end
end
end
form do |f|
f.inputs "Add/Edit Post" do
f.input :title
f.input :content
f.input :categories, :as => :check_boxes
end
f.buttons
end
@chewmanfoo
Copy link

I saw your article on your blog about this code and I wanted to ask a question - I hope you have time to answer :)

Suppose your categories had more useful information within them than just the name. For example, suppose you had two 'Political' categories, one for local and one for national politics. You'd want to show the user the description field of the Category as well as the name field. Can you do this with this form format? Something like this:

form do |f|
f.inputs "Add/Edit Post" do
f.input :title
f.input :content
f.input :categories, :as => :check_boxes do |c|
magically_show c.description
end
end

Any way to do this? In my project, I am merging svn revisions into an integration branch - so the merge task has checkboxes for revisions, but I'd like to show the user all the files affected by the revision so they can click the revision numbers they want based on the files. I'm trying to make this as elegant as possible, making full use of the syntax provided here.

Any help would be appreciated!
Chewy

@skorbut
Copy link

skorbut commented Jan 2, 2013

Hello Chewy,

a little late for an answer after 3 months, but you can provide a :member_label => :name parameter to point to another method on the models within the collection. In your case it would be

f.input :categories, :as => :check_boxes, :member_label => :description

I got this information from the formtastic sources used by Active Admin

https://github.com/justinfrench/formtastic/blob/master/lib/formtastic/inputs/check_boxes_input.rb

Regards,
Karsten

@CraneWing
Copy link

I am doing my first Rails app and have a join table that is used for many-to-many relationships of Recipes and their Categories. For the join table to work, I have read on other blogs not to have auto-incrementing IDs on each table line. So the join table has only category_id and recipe_id. Now standard RESTful routing for editing an item is to pass an ID for the record. Without any IDs, how can I create an edit page for the lines in this join table? Thanks for any assistance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment