-
-
Save airhorns/482051 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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