Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActiveAdmin display collection in Show method

I have Packages, which have multiple Components (linked using a has_many through Bundles)

In ActiveAdmin, when I show a Package I want to be able to show all the Components linked to it

So I have a show method as follows:

show do
 attributes_table do
  row :description

  row 'Components' do |n|
     package.components.each do |component|
       #debugger
       component.name
     end
  end
 end
end

When I display the page, it shows the full version of each record, ie (one of which I show below, but there will be as many as there are Components):

[#<Component id: 2, component_token: "6e9be0b0-71c0-012f-d523-00254bca74c4", name: "Exercise Module", description: "This is the exercise module", created_at: "2012-04-26 11:25:20", updated_at: "2012-04-26 11:25:20">]

When I stop the debugger at the point I have commented it, the value for component.name is given as "Exercise Module", but that is not what is outputted to the show - in fact, ActiveAdmin seems to ignore everything in that |component| block.

How do I display the record attributes, and not the whole record itself?

Thanks

like image 977
Mitch Avatar asked Apr 26 '12 12:04

Mitch


People also ask

How to add a collection action in activeadmin?

To add a collection action, use the collection_action method: ActiveAdmin.register Post do collection_action :import_csv, method: :post do # Do some CSV importing work here... redirect_to collection_path, notice: "CSV imported successfully!"

How do I render custom controller actions in active admin?

Custom controller actions support rendering within the standard Active Admin layout. If you would like to use the same view syntax as the rest of Active Admin, you can use the Arbre file extension: .arb. For example, create app/views/admin/posts/comments.html.arb with:

Why do we have to provide the ID for activeadmin?

We have to provide a tuple with the string representation and the id. We have to provide the id for ActiveAdmin to be able to look up and filter by a particular developer. ActiveAdmin does its best to infer the type of input that should be used for a particular attribute, but it doesn't always nail it on the head.


1 Answers

It's happening because the row shows the output of this line package.components.each {|component| ... } and that's the collection

Try this:

show do

 attributes_table do
  row :description

  row 'Components' do |n|
     package.components.map(&:name).join("<br />").html_safe
  end
 end

end

or any other join string you prefer :)

like image 171
alony Avatar answered Oct 21 '22 02:10

alony