Skip to content

Instantly share code, notes, and snippets.

@airhorns
Forked from jeffdeville/gist:482032
Created July 19, 2010 21:31
Show Gist options
  • Save airhorns/482051 to your computer and use it in GitHub Desktop.
Save airhorns/482051 to your computer and use it in GitHub Desktop.
# model
class GiftIdea
has_many :images
end
# in the view (say app/views/gift_ideas/show.html.erb)
# say we are rendering html about one GiftIdea object, found in the @gift_idea variable
..
..
<div class="gift_idea">
<%= link_to @gift_idea.title, @gift_idea.url %>
<img src="<%= gift_idea_image_url(@gift_idea) %>">
</div>
..
..
# in GiftIdeasHelper (app/helpers/gift_ideas_helper.rb)
def gift_idea_image_url(idea)
if idea.images.present?
idea.images.first.url
else
"/path/to/generic/image.jpg"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment