I have some code in one of my views like this. I feel that it has too many if else conditions. All it does is checking for the ActiveRecord Object’s status and setting the image. Is there a better way to do the following in Rails?
<td class="notification_msg">
<% if notification.status == NOTIFICATION_STATUS.index("Failure") %>
<img src="images/failure.png" style="vertical-align: middle"/>
<% elsif notification.status == NOTIFICATION_STATUS.index("Success") %>
<img src="images/success.png" style="vertical-align: middle"/>
<% elsif notification.status == NOTIFICATION_STATUS.index("Warning") %>
<img src="images/warning.gif" style="vertical-align: middle"/>
<% else %>
<img src="images/unknown.gif" style="vertical-align: middle"/>
<% end %>
<%= notification.message %> <%= cta_links_for(notification) -%>
</td>
Thanks
I’d pull the logic into a helper. Your erb:
Then in your view helper:
And of course, no inline styles. So put
.notification_msg img{vertical-align:middle;}in your css file.