简体   繁体   中英

rails - passing method name to helper function

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 internationalization 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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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