Reputation: 956
I am using Active admin gem in my rails app. I added resources book which has 20 columns, now i need to customize only one column and print the remaining as it is. I tried below code
ActiveAdmin.register Book do
index do
column :description do
raw "<a class='view_description button'>View Description</a>"
end
end
end
but which hides all the columns and show only description. Any help will be useful.
Upvotes: 18
Views: 20953
Reputation: 3077
I was curious about this question too. Here is what I found
index do
column :id
active_admin_config.resource_columns.each do |attribute|
column attribute
end
end
Upvotes: 6
Reputation: 25064
This is pretty accurate to mimic original behavior in a generic way:
index do
selectable_column
Book.column_names.each do |c|
# Filter foreign IDs and counter caches
column c.to_sym unless c.ends_with?('_id') || c.ends_with?('_count')
end
Book.reflect_on_all_associations(:belongs_to).map(&:name).each do |association|
column association
end
# Whatever custom thing you want
actions
end
Upvotes: 0
Reputation: 222
In my case, I want only rename the one column, I have done this way ->
index do
column :one
column :two
....
column "View Description", :description # This will change you column label **description** to **View Description**
end
Upvotes: 11
Reputation: 1254
In General, this is quite easy..
ActiveAdmin.register Book do
index do
(Book.column_names - ["column_to_customize"]).each do |c|
column c.to_sym
end
column :column_to_customize do
raw "<a class='view_description button'>View Description</a>"
end
end
end
Upvotes: 2
Reputation: 710
This also works when you want add or customize just a single column to the default list (based on an association which is for a belongs_to).
ActiveAdmin.register Book do
index do
column :publisher do |book|
book.publisher.name
end
Book.column_names.each do |c|
column c.to_sym
end
end
end
Upvotes: 3
Reputation: 611
How about this?
ActiveAdmin.register Book do
index do
columns_to_exclude = ["name"]
(Book.column_names - columns_to_exclude).each do |c|
column c.to_sym
end
column :description do
raw "<a class='view_description button'>View Description</a>"
end
end
end
Upvotes: 46
Reputation: 264
If you specify an index block, you need to put all the columns that you want to show, because you are replacing the "default" behaviour.
In your case, you need to add the other 19 columns with something like:
ActiveAdmin.register Book do
index do
column :one
column :two
column :three
column :name
column :title
column :pages
column :description do
raw "<a class='view_description button'>View Description</a>"
end
end
end
Upvotes: 13