rails_admin 显示名称而不是 id [英] rails_admin display name instead of id

查看:63
本文介绍了rails_admin 显示名称而不是 id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将 rails_admin 安装到我的应用程序中,并且我想做一些非常基本的事情...我有两个模型,它们的关联按预期出现...我有一个属于 :user 的研讨会注册模型.

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.

在 rails_admin 中,它将我的研讨会注册用户列为用户 #1、用户 #1 等.

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

pretty_value"部分为我提供了用户的名字和姓氏的文本...但有两个问题:

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

1) 它不再是一个链接.如果我保留默认值(用户 #1、用户 #2 等),它会提供指向该用户的链接.怎么找回那个链接?rails_admin 如何定义它的路径?

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) 必须在我的表单中按 id 查找似乎非常笨重...

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

抱歉,这是一个基本问题.我已经阅读了手册并查找了其他问题,但对我来说还没有完全点击".我对 Rails 也很陌生.

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.

谢谢.

我必须这样做才能让它与链接一起工作:

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

我按照建议为全名添加了一个辅助方法,但将其保留在我的视图 helpers 中:

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

然后,我像这样更改了pretty_value"部分:

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

基本上,要访问任何视图助手(制作的导轨或其他方式),您必须添加 indings[:view].my_tag_to_use

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

要获取用户的 rails_admin 路由,例如您可以执行以下操作:

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

bindings[:view].rails_admin.show_path('user', user_id)

推荐答案

我在 google 上偶然发现了这个问题,并找到了一个更简单的方法来做到这一点.将 titlename 方法添加到您的模型中,rails_admin 将使用该方法而不是显示User #1".

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

您可以使用 title 而不是 name,但在您的情况下,使用 name 更有意义.

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

这篇关于rails_admin 显示名称而不是 id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆