ruby on rails - What is the best way to check if an attribute exists and is set? -


i have common view lists 2 different models. difference when setting link_to action, 1 of models has link attribute , other doesn't. want check if link attribute exists, , if does, check if it's set. have following works, wondering if there better way.

%li   - if @element.has_attribute?("link") && @element.link     = link_to @element.title, @element.link   - else     = link_to @element.title, @element 

you use presence:

= link_to @element.title, (@element.link.presence || @element) 

or, if @element might not have link @ all, use try:

= link_to @element.title, (@element.try(:link) || @element) 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -