Using Rails 3. This is a front-end design question.
Goal:
Contact | Email | URL
show.html.erb:
<% if !@shop.contact.blank? %>
<%= @shop.contact %>
<% end %>
<% if !@shop.email.blank? %>
<%= @shop.email %>
<% end %>
<% if !@shop.url.blank? %>
<%= link_to @shop.url, @shop.url, :target => "_blank" %>
<% end %>
How do I put in | only when the previous and after element has values? At current stage, if there is no value, nothing is output.
Many thanks.
This creates an array of all your elements, selects those which have a value (as
present?is the opposite ofblank?) and then joins each element of the remaining array by putting a pipe between them.That said, if you have more complex logic, you should probably create a helper method. The ERB templates are not a good place for complex logic. This above is bordering acceptable.