I'm trying to write a single line if else statement in a view.
<%= collection.name ? collection.name : @miniature.name %>
I want it to put collection.name if one exists, otherwise I want it to put @miniature.name
I'm trying to write a single line if else statement in a view.
<%= collection.name ? collection.name : @miniature.name %>
I want it to put collection.name if one exists, otherwise I want it to put @miniature.name
Check for the presence of
collection.name
first.Couldn't you have
To those who downvoted - Set Ruby variable if it is not already defined
To make it even clearer, you can use logical
OR
andActiveSupport's
Object#presence
(to putcollection.name
only if it exists and isn't blank):If you want to display
collection.name
if it's notnil
, but it's blank (empty string or string containing only whitespace), it will be enough to have: