Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

html_safe with undefined fields

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 to apply only if the field is defined? thanks

like image 368
newbie_86 Avatar asked May 18 '26 23:05

newbie_86


1 Answers

In Rails, you can use

myfield.try(:html_safe)

Docs: http://api.rubyonrails.org/classes/Object.html#method-i-try

like image 78
Nikola Avatar answered May 21 '26 15:05

Nikola



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!