patternhtmlMinor
Showing items as in a Carousel
Viewed 0 times
itemsshowingcarousel
Problem
Is there a better way to implement my carousel in less lines?
" alt="">
TITLE
" alt="">
" alt="">
" alt="">
" alt="">
" alt="">
Solution
Get rid of unnecessary code:
Move stuff to helpers:
" alt="">
TITLE
" alt="">
Move stuff to helpers:
module PhotoHelpers
def slider_image(photo)
image_tag photo.image_url(:large), alt: photo.title
end
end
TITLE
Code Snippets
<div id="myCarousel" class="carousel slide">
<div class="carousel-inner">
<% @photos.each_with_index do |photo, index| %>
<% if index.zero? %>
<div class="item active">
<img src="<%= photo.image_url(:large) %>" alt="<%= photo.title %>">
<div class="container">
<div class="carousel-caption">
<h1>TITLE</h1>
</div>
</div>
</div>
<% else %>
<div class="item">
<img src="<%= photo.image_url(:large) %>" alt="<%= photo.title %>">
</div>
<% end %>
<% end %>
</div>
</div>module PhotoHelpers
def slider_image(photo)
image_tag photo.image_url(:large), alt: photo.title
end
end
<div id="myCarousel" class="carousel slide">
<div class="carousel-inner">
<% @photos.each_with_index do |photo, index| %>
<% if index.zero? %>
<div class="item active">
<%= slider_image photo %>
<div class="container">
<div class="carousel-caption">
<h1>TITLE</h1>
</div>
</div>
</div>
<% else %>
<div class="item">
<%= slider_image photo %>
</div>
<% end %>
<% end %>
</div>
</div>Context
StackExchange Code Review Q#20750, answer score: 4
Revisions (0)
No revisions yet.