A
A
Alexander Grishin2016-02-25 20:03:08
Ruby on Rails
Alexander Grishin, 2016-02-25 20:03:08

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

1 answer(s)
E
Ernest Faizullin, 2016-02-25
@beerdy

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

and call it in the view:
<℅ products.each do |product| %>
    <img src"<%= product.get_photo_url %>" alt="">
<% end %>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question