Answer the question
In order to leave comments, you need to log in
How to program a block of code inside an iterator outside of a View?
<℅
products.each do |product|
photo = product.photo.first
url = 'no photo'
url = photo.image.url if photo
%>
Answer the question
In order to leave comments, you need to log in
probably create a get_photo_url method in the model:
class Product
...
def get_photo_url
@product.photo.first ? @product.photo.first.image.url : '/images/no-photo.jpg'
end
...
end
<℅ products.each do |product| %>
<img src"<%= product.get_photo_url %>" alt="">
<% end %>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question