Adding an additional column value in a list on RailsAdmin
Asked Answered
L

2

5

I have RailsAdmin running for my Rails 3 app which has two models - Sale and Merchandise. There's a HABTM relationship between the two. In RailsAdmin, when a sale is added or edited, a list of available merchandises is shown in the usual fashion. However, only the "name" column of the merchandise is shown. I have another column whose value needs to be included for the list to make any sense. How do I add this to the RailsAdmin interface?

I understand that the RailsAdmin docs say that field declarations have access to a bindings hash which contains the current record instance -- but I can't find any examples of how to implement this. Thanks for any help.

Lauds answered 16/2, 2012 at 13:40 Comment(0)
M
5

I'd propose that you use the custom object label method for this. Your RailsAdmin config might look like this:

config.model Merchandise do
  object_label_method
    :custom_label
  end
end

And your ActiveRecord model would contain a method for the instance labels:

class Merchandise < ActieRecord::Base
  def custom_label
    "#{self.label} #{self.another_column} #{self.another_column2}"
  end
end

This doesn't answer your question about the available bindings variables, but I hope it addresses the root question. If you want to see what variables are accessible in a custom field view, you can look through the views in ~/rails_admin/app/views/rails_admin/main/. A quick grep shows that bindings[:object] is accessible in those views, but IIRC, there are a few other bindings variables that are accessible.

Matheny answered 22/2, 2012 at 4:46 Comment(1)
Just want to add for others, the object_label_method can be added directly inside the model so you can keep all configurations within the particular model (and as an added bonus you won't have to restart your server for new config change).Trotskyism
M
5

You have at least the following objects available:

bindings[:object] # the actual object
bindings[:view]   # you can access view helpers here
bindings[:controller]  # you can access the controller

What you need in this case is bindings[:object]

Martensite answered 18/7, 2012 at 15:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.