Pavel K.
Pavel K.

Reputation: 6817

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

Upvotes: 1

Views: 448

Answers (1)

sikachu
sikachu

Reputation: 1221

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

Upvotes: 1

Related Questions