i have a field which is not required i.e. can be nil. i want to use the ffg: myfield.html_safe in my view. this doesn't work for the items that have no myfield. i get an exception, how do i get html_safe 开发者_如何学Pythonto apply only if the field is defined? thanks
In Rails, you can use
myfield.try(:html_safe)
Docs: http://api.rubyonrails.org/classes/Object.html#method-i-try
myfield.html_safe if myfield
or myfield.to_s.html_safe
This should do it...
myfield.html_safe unless myfield.blank?
精彩评论