rails_admin display name instead of id
Asked Answered
R

6

24

I've installed rails_admin into my app and I want to do something pretty basic...I have two models and their association comes up as expected...I have a seminar registration model that belongs_to :user.

In the rails_admin it lists my seminar registration users as User #1, User #1, etc.

I'd like to have that be the user's name instead. What I've managed to do is this:

config.model SeminarRegistration do
label "Seminar Signups"
# Found associations:
  configure :user, :belongs_to_association 
  configure :seminar_time, :belongs_to_association   #   # Found columns:
  configure :id, :integer 
  configure :user_id, :integer         # Hidden 
  configure :seminar_time_id, :integer         # Hidden 
  configure :created_at, :datetime 
  configure :updated_at, :datetime   #   # Sections:

list do
  field :user do
    pretty_value do
     user = User.find(bindings[:object].user_id.to_s)
     user.first_name + " " + user.last_name
    end
  end
  field :seminar_time
end
export do; end
show do; end
edit do; end
create do; end
update do; end
end

The "pretty_value" section gives me the text of my user's first and last name...but has two problems:

1) It is no longer a link. If I leave the default value (User #1, User #2, etc) it provides a link to that user. How do I get that link back? How does rails_admin define it's paths?

2) Seems awfully clunky to have to look up by id right there in my form...

Sorry if this is a basic question. I've read the manual and looked up other questions but it hasn't quite "clicked" for me yet. I'm also pretty new to rails.

Thanks.


I had to do this to get it to work with the link:

I added a helper method for the full name as suggested, but kept it in my view helpers:

module ApplicationHelper
 def full_name(user_id)
  user = User.find(user_id)
  user.first_name + " " + user.last_name
 end
end

Then, I changed the "pretty_value" section like so:

pretty_value do
  user_id = bindings[:object].user_id
  full_name = bindings[:view].full_name(user_id)
  bindings[:view].link_to "#{full_name}", bindings[:view].rails_admin.show_path('user', user_id)
end

Basically, to get access to any view helpers (rails made or otherwise) you have to add indings[:view].my_tag_to_use

To get the rails_admin route for a user, for example you can do:

bindings[:view].rails_admin.show_path('user', user_id)
Richardricharda answered 25/7, 2012 at 20:42 Comment(0)
G
38

I stumbled upon this question on google and found a much simpler way to do this. Add a title or name method to your model, which rails_admin will use instead of displaying "User #1".

class User
  ...
  def name
    first_name + " " + last_name
  end
  ...
end

You can use title instead of name, but in your case it makes more sense to use name.

Gissing answered 3/2, 2014 at 11:44 Comment(5)
the best hack ever ! :DCloying
absolutely brilliant. This should be the accepted answer.Plexiform
This works but if I go to user and try to create a new one, I get undefined method +' for nil:NilClassEchinoid
Thank you for solution, and I have managed to work in List and Show action but at New and Edit (Inside dropdown) I still not getting full name please check: postimg.org/image/cpiqe9o5d @Brice Durand What am I doing wrong here ?Nitin
field :user_id, :enum do enum do User.employee.collect {|p| [p.full_name, p.id]} end this helped me with dropdown. Thanks on more time!Nitin
B
28
RailsAdmin.config {|c| c.label_methods << :full_name}

or

config.model full_name do
  object_label_method do
    :full_name
  end
end

Then add a full_name method in your model.

Bowser answered 14/9, 2012 at 17:15 Comment(3)
Ok, that's a cleaner way of doing it. But didn't get me the link...I edited my answer above to show how I got it working.Richardricharda
It works but in association lookups you can only search based on either first or last name, but not both together.Nashua
I also get a link with object_label_method, association just have to be definedChitkara
S
10

I do this like for 'Role' model

config.model 'Role' do
  object_label_method do
    :custom_label_method
  end
end

def custom_label_method
  "#{role_name}"
end

It works

Sunny answered 10/4, 2014 at 13:36 Comment(1)
This was exactly what I needed. I would add a bit of context for others that you should add the "config.model" to the rails_admin.rb initializer and the custom_label_method to the model class.Abracadabra
R
7

You can use rails_admin object_label_method

See link

For User model, in rails_admin.rb

config.model 'User' do
  object_label_method do
   :custom_label_method
  end
end

In model create method

def custom_label_method
  "User #{user_name}"
end
Revelation answered 16/9, 2017 at 8:9 Comment(0)
S
2

Simply add this in your User Model :

# Prety User name display for rails_amdin
def to_s
    return self.name
end
def title
    return self.to_s
end

Then restart server.

You must add both to_s and title method and you can change self.name by whatever you want from your User schema.

It works for me with Rails 4.

Segalman answered 10/7, 2015 at 11:43 Comment(1)
You won't need to_s. Defining title or name will be enough, see the source hereCattleman
D
0

With something as generic as name I usually add a method on the model. But alternative solution is to configure label for the field in RA

Dyspnea answered 13/1, 2016 at 1:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.