开发者

rails - passing method name to helper function

开发者 https://www.devze.com 2022-12-14 19:43 出处:网络
docs say that options_from_collection_for_select should be used following way: options_from_collection_for_select(collection, value_method, text_method, selected = nil)

docs say that options_from_collection_for_select should be used following way:

options_from_collection_for_select(collection, value_method, text_method, selected = nil) 

so, in my case for example

options_from_collection_for_select(@messages,'id','title')

but i need to put more information to title, so what i tried to do was:

class Message < ActiveRecord::Base

 def proper_title
  self.name+", updated at "+self.updated_at
 end

end

and it works, but thing is i need strings internationalized and it's a bit more difficult with models than with controllers. now do i have to do model internationali开发者_开发问答zation in this case or is it possible to get around somehow? thanks


You can still call I18n.translate() in the model. It will give you the same result as t() helper

# 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')
0

精彩评论

暂无评论...
验证码 换一张
取 消