17

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.

4

7 回答 7

42

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
于 2013-04-19T18:18:51.617 回答
13

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
于 2013-04-19T02:53:36.470 回答
7

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
于 2019-03-28T07:41:56.343 回答
4

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

https://github.com/activeadmin/activeadmin/blob/5dbf9b690302ecb4ba0c0ce59b2fb4735c88b35c/lib/active_admin/views/index_as_table.rb#L261

于 2020-07-16T21:32:24.063 回答
3

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
于 2016-01-06T16:37:05.840 回答
2

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
于 2017-09-25T09:29:13.103 回答
0

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
于 2019-12-02T15:14:10.187 回答