In my app I have venue records where each one can have many photos and reviews.
How can I go about displaying a 'nothing entered yet' message where the photos or reviews should be displayed when there isn't any?
Heres how I'm currently displaying them:
Review
<div id="reviews">
<%= render :partial => 'reviews/review', :collection => @venue.reviews %>
</div>
Photos (displayed using colorbox)
<div class="venue_photos_container">
<div class="ppy4" id="ppy4">
<ul class="ppy-imglist">
<% for venuephoto in @venue.venuephotos %>
<li class="venue_photo_thumb_container"><%= link_to image_tag(venuephoto.venuephoto.url(:image_scroller), :class => "venue_photo"), venuephoto.venuephoto.url(:large), :rel => "venue_photo_colorbox" %></li>
<% end %>
</ul>
<div class="ppy-outer">
<div class="ppy-stage">
<div class="ppy-nav">
<a class="ppy-prev" title="Previous image">Previous image</a>
<a class="ppy-switch-enlarge" title="Enlarge">Enlarge</a>
<a class="ppy-switch-compact" title="Close">Close</a>
<a class="ppy-next" title="Next image">Next image</a>
开发者_开发百科 </div>
</div>
</div>
</div>
</div>
Thanks for any help its much appreciated!
I would count the venue photos and run the count through a conditional statement. If it's zero display "no pics" otherwise display the photos.
<div class="venue_photos_container">
<div class="ppy4" id="ppy4">
<% if @venue.venuephotos.count.zero? %>
Sorry, no pictures!
<% else %>
<ul class="ppy-imglist">
<% for venuephoto in @venue.venuephotos %>
<li class="venue_photo_thumb_container"><%= link_to image_tag(venuephoto.venuephoto.url(:image_scroller), :class => "venue_photo"), venuephoto.venuephoto.url(:large), :rel => "venue_photo_colorbox" %></li>
<% end %>
</ul>
<div class="ppy-outer">
<div class="ppy-stage">
<div class="ppy-nav">
<a class="ppy-prev" title="Previous image">Previous image</a>
<a class="ppy-switch-enlarge" title="Enlarge">Enlarge</a>
<a class="ppy-switch-compact" title="Close">Close</a>
<a class="ppy-next" title="Next image">Next image</a>
</div>
</div>
</div>
<% end %>
</div>
</div>
Just a disclaimer, this code demonstrates the basic idea but I use HAML so this ERB may not be exactly correct.
An alternate way that I do this that keeps me from having if/then logic in my views is this:
In Venue.rb:
class Venue
def photo_state
venuephotos.count > 0 ? 'with_photos' : 'without_photos'
end
end
In show.html.erb:
<%= render("venue_#{photo_state}" %>
In _venue_with_photos.html.erb:
<ul class="ppy-imglist">
<%= render @venue.venuephotos %>
</ul>
In _venue_without_photos.html.erb:
Sorry, no pictures!
In _venuephoto.html.erb
<li class="venue_photo_thumb_container"><%= link_to image_tag ... %></li>
Yes, a bit obsessive, but I like having no code except "render" in my views whenever possible. It's all in whether you like all your code in one file or each piece broken out in easy-to-digest chunks. A preference thing - no real right answer.
精彩评论