我已将 rails_admin 安装到我的应用程序中,我想做一些非常基本的事情...我有两个模型,它们的关联按预期出现...我有一个属于_to :user 的研讨会注册模型。
在 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
基本上,要访问任何视图助手(制作的导轨或其他),您必须添加 indings[:view].my_tag_to_use
要获取用户的 rails_admin 路由,例如,您可以执行以下操作:
bindings[:view].rails_admin.show_path('user', user_id)