[英]rails - passing method name to helper function
文档说应该按以下方式使用options_from_collection_for_select:
options_from_collection_for_select(collection, value_method, text_method, selected = nil)
因此,以我为例
options_from_collection_for_select(@messages,'id','title')
但是我需要在标题上添加更多信息,所以我试图做的是:
class Message < ActiveRecord::Base
def proper_title
self.name+", updated at "+self.updated_at
end
end
并且它可以工作,但是我需要国际化的字符串,并且使用模型要比使用控制器更困难。 现在,在这种情况下,我是否必须进行模型国际化,还是有可能解决? 谢谢
您仍然可以在模型中调用I18n.translate()
。 它将为您提供与t()
帮助器相同的结果
# Message.rb
def proper_title
I18n.translate("message.proper_title", :name => self.name, :updated_at => self.updated_at)
end
# en.yml
en:
message:
proper_title: "{{name}}, updated at {{updated_at}}"
# view
options_from_collection_for_select(@messages,'id','proper_title')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.