简体   繁体   English

Rails 3和ActiveAdmin:to_s与display_name

[英]Rails 3 and ActiveAdmin: to_s vs. display_name

I'm trying to get my head around Rails and ActiveAdmin, and string representations of objects. 我试图让我了解Rails和ActiveAdmin以及对象的字符串表示形式。 Particularly, I'm struggling to define a single method that'll get picked up by Rails (in the templates) and ActiveAdmin. 特别是,我正在努力定义一个将由Rails(在模板中)和ActiveAdmin选用的方法。

If I do something like: 如果我做类似的事情:

def to_s
  "Hello world"
end

Then that works in Rails. 然后在Rails中工作。 But not in ActiveAdmin, which only picks it up if I use display_name . 但是在ActiveAdmin中则不是,ActiveAdmin仅在使用display_name时才选择它。 It's solved by doing this: 这样做可以解决:

alias_attribute :to_s, :display_name

But is that a bit hacky? 但这有点hacky吗? Just wondering if I've missed something obvious. 只是想知道我是否错过了明显的事情。 Thanks! 谢谢!

In the end, I used: 最后,我使用了:

def display_name
  "#{name}"
end

What about calling the method something else like 调用该方法又如何呢?

def something
  "Hello World"
end

and then calling... 然后打电话给...

alias_attribute :something alias_attribute:某事

Perhaps there is a conflict with calling the method to_s in this case? 在这种情况下,调用方法to_s可能存在冲突?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM