Adding an Optional Column Value to the RailsAdmin List

My RailsAdmin works for my Rails 3 application, which has two models - Sale and Merchandise. There is a HABTM relationship between them. In RailsAdmin, when a sale is added or edited, the list of available products is shown in the usual way. However, only the “product name” column is displayed. I have another column whose value needs to be included in the list in order to make sense. How to add this to the RailsAdmin interface?

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

+5
source share
2 answers

I would suggest using the custom label label method for this. The RailsAdmin configuration might look like this:

config.model Merchandise do
  object_label_method
    :custom_label
  end
end

And your ActiveRecord model will contain a method for instance labels:

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

This does not answer your question about available binding variables, but I hope it addresses the root question. If you want to see which variables are available in a custom field view, you can view the views in ~ / rails_admin / app / views / rails_admin / main /. A quick grep shows that [: object] bindings are available in these views, but IIRC, there are several other binding variables that are available.

+5
source

You have at least the following objects:

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

In this case you will need bindings[:object]

+5
source

All Articles