问题
I have a model UserDelegation that contains:
belongs_to :delegator, :class_name => 'User'#, :conditions => {:order => 'users.last_name ASC, users.first_name ASC'}
belongs_to :delegatee, :class_name => 'User', :touch => true#, :conditions => {:order => 'users.last_name ASC, users.first_name ASC'}
and in my user model i have this:
has_many :delegatees, :through => :user_delegatees, :order => 'users.last_name ASC, first_name ASC'
has_many :delegators, :through => :user_delegators, :order => 'last_name ASC, first_name ASC'
when i am in rails admin and go to the User delegation table and add a new one i get two drop downs which lists the users name. I do know that rails_admin uses name by default when it populates drop downs in associations.
What i am wanting to know is how to override what is displayed in these drop downs so I could for example have it display 'user's name - user's location'?
回答1:
Change the object_label_method
of your User
model for rails_admin
class User < ActiveRecord::Base
...
rails_admin do
object_label_method :name_location
end
def name_location
self.name + ' - ' + self.location
end
end
回答2:
Joe Kennedy's answer works perfectly. But when I use
class User < ActiveRecord::Base
rails_admin do
object_label_method :name_location
end
end
the following codes in config/initializers/rails_admin.rb were neglected.
RailsAdmin.config do |config|
config.model 'User' do
...
end
end
So I did
RailsAdmin.config do |config|
config.model 'User' do
object_label_method :name_location
...
end
end
class User < ActiveRecord::Base
def name_location
self.name + ' - ' + self.location
end
end
and this works.
回答3:
You will need to specify it in the admin/user_delegation.rb
file.
There you will have the fields of your form. Find the drop-down field and modify it:
f.input :your_dropdown_field, :as => :select, :collection => User.all.collect{|u| [u.name.to_s + " - " + u.location.to_s] }
Replace :your_dropdown_field
, u.name
and u.location
with your values.
来源:https://stackoverflow.com/questions/21751845/how-to-override-value-that-appears-in-a-dropdown-in-the-rails-admin-gem