简体   繁体   English

在 Rails 中,to_label 和 to_s 有什么区别?

[英]In Rails, what's the difference between to_label and to_s?

In Rails, what's the difference between to_label and to_s ?在 Rails 中, to_labelto_s有什么区别? Especially in the context of displaying model info in views.特别是在视图中显示 model 信息的情况下。 I know that if you define to_s you don't have to explicitly call Model.to_s in views.我知道,如果您定义to_s ,则不必在视图中显式调用Model.to_s What's the advantage of using to_label then?那么使用to_label有什么好处呢?

to_label has been deprecated, you can't find it from the Rails3 source to_label 已被弃用,您无法从 Rails3 源代码中找到它

I believe the difference was that to_label did some magic in that converting values such as object class names to_s where as to_s just converts an object to a string.我相信不同之处在于to_label在转换诸如 object class 名称to_s之类的值方面起到了一些作用,而to_s只是将 object 转换为字符串。

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

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