2012-07-25 251 views
19

我已经安装了rails_admin到我的应用程序,我想做一些非常基本的...我有两个模型,他们的联系按预期出现......我有一个研讨会注册模型belongs_to :用户。rails_admin显示名称而不是id

在rails_admin它列出了我的研讨会注册用户为用户#1,用户#1等

我想有一个是用户的名称,而不是。我已经成功地做到的是:

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”节给了我我的用户名和姓的文字...但有两个问题:

1)这是没有更长的链接。如果我离开默认值(用户#1,用户#2等),它提供了一个链接到该用户。我如何获得该链接? rails_admin如何定义路径?

2)看起来太过笨重有通过ID就在那里看在我的形式...

很抱歉,如果这是一个基本的问题。我已阅读手册并查找了其他问题,但尚未完全“点击”我。我对rails也很新颖。

谢谢。


我不得不这样做是为了得到它与链接工作:

我添加了作为建议的全名的辅助方法,但保留在我的视图助手:

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

然后,我喜欢这样的“pretty_value”部分改变:

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 

基本上,以访问任何观点,他lpers(导轨或其他方式作出)必须添加indings [:视图] .my_tag_to_use

要获取用户的rails_admin路线,例如,你可以这样做:

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

回答

20
RailsAdmin.config {|c| c.label_methods << :full_name} 

config.model full_name do 
    object_label_method do 
    :full_name 
    end 
end 

然后在模型中添加一个full_name方法。

+0

好吧,这是一个更干净的方式。但没有得到我的链接...我编辑我的答案上面,以显示我如何得到它的工作。 – user1535082 2012-09-24 20:44:47

+0

它可以工作,但在关联查找中,您只能根据名字或姓氏进行搜索,但不能同时进行搜索。 – 2014-02-18 04:48:20

+0

我还得到一个object_label_method的链接,关联只需要定义 – 2014-08-23 13:06:57

30

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

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

可以使用title代替name,但在你的情况下,它更有意义,使用的名字。

+0

真棒和简单的答案,thx很多! – GuiGreg 2014-04-30 13:39:29

+0

有史以来最好的黑客! :D – AshwinKumarS 2014-10-15 12:19:02

+1

绝对精彩。这应该是被接受的答案。 – Brit200313 2015-06-10 23:08:02

9

我这样做像 '角色' 模式

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

def custom_label_method 
    "#{role_name}" 
end 

它的工作原理

+1

这正是我所需要的。我会为其他人添加一些上下文,您应该将“config.model”添加到模型类的rails_admin.rb初始化程序和custom_label_method中。 – 2015-06-19 18:08:29

0

只需在您的用户模型补充一点:

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

然后,重新启动服务器。

您必须同时添加to_stitle方法,并且您可以通过用户方案中的任何内容更改self.name

它为我使用Rails 4

+0

你不需要'to_s'。定义'title'或者'name'就足够了,看到这里的源代码(https://github.com/sferik/rails_admin/blob/12aeaf30b2148d3cbebb2f831132b4787fd47bfe/lib/rails_admin/config.rb#L50) – 2016-02-15 04:29:56

0

的东西作为一般的名字,我平时在模型上添加一个方法。但是,另一种方法是在RA

0

您可以使用rails_admin object_label_method

See link

对于用户模型,在rails_admin.rb为外地配置标签

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

在模型建立方法

def custom_label_method 
    "User #{user_name}" 
end 
相关问题