Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can we create partials in active admin for index panel

I want to create a partials to render some data in index panel for different namespaces for DRY

I am currently writing

 index do
    render 'index', user: :user    
 end
//_index.html.arb
column :id
column 'Customer Name', :name
column :mobile
column :recipient_number    
column :cash_in_hand do |customer|
  number_to_currency(customer.cash_in_hand, unit: "\u20B9", precision: 2)
end
column "Due Balance" do |customer|      
  number_to_currency(customer.due_balance, unit: "\u20B9", precision: 2)
end
actions
like image 997
Mukesh Avatar asked Dec 31 '15 05:12

Mukesh


People also ask

How do I view pictures as active admin?

To get the image file uploading work with Active Admin you need to specify your own form partial. Luckily for us, Active Admin provides a clean way of doing this. You just specify a partial name in a Active Admin resource configuration and whitelist the image param.

What is Active Admin rails?

Active Admin is a Ruby on Rails plugin for generating administration style interfaces. It abstracts common business application patterns to make it simple for developers to implement beautiful and elegant interfaces with very little effort.


2 Answers

You can create a partial to render that data exactly like this

# app/admin/some_class.rb
index do
  render 'admin/index', context: self    
end

You probably will want to create a folder called 'admin' in views for these types of partials ...

# app/views/admin/_index.html.erb
<% context.instance_eval do
  column :id
  column 'Customer Name', :name
  column :mobile
  column :recipient_number    
  column :cash_in_hand do |customer|
    number_to_currency(customer.cash_in_hand, unit: "\u20B9", precision: 2)
  end
  column "Due Balance" do |customer|      
    number_to_currency(customer.due_balance, unit: "\u20B9", precision: 2)
  end
  actions
end %>

I can confirm this works with .erb extention file and .haml but no guaranteeing others

like image 154
MilesStanfield Avatar answered Sep 22 '22 08:09

MilesStanfield


This works for me in Rails 6 with:

#app/admin/some_class.rb
index do
  render partial: 'active_admin/index', locals:{context: self}
end

and

#app/views/active_admin/_index.html.arb
context.instance_eval do
  selectable_column
  id_column
  column :some_attribute
  actions
end

This also satisfies locating the file in a shared place not specific to the admin/ space. It's useful because I have AA running with two separate user models and I often want to share the same code for index, show, etc with minor differences.

If needed, additional key-values can be passed in the locals hash for minor customization of the results.

like image 40
Eli R. Avatar answered Sep 23 '22 08:09

Eli R.