alpheus
alpheus

Reputation: 989

Adding an additional column value in a list on RailsAdmin

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.

Upvotes: 5

Views: 2383

Answers (2)

rewritten
rewritten

Reputation: 16435

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]

Upvotes: 5

user1186625
user1186625

Reputation:

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.

Upvotes: 5

Related Questions