10
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

RailsでBootstrapのスライドショーPartial

Last updated at Posted at 2017-08-10

Rails用にBootstrapのCarouselのPartialを作成したのでメモ。
RailsにBootstrapを導入する方法はこちらを参照。

スクリーンショット 2017-08-10 12.26.46.png

画像は_headerという接尾語をつけて区別する。
例 image1_header.jpg

home.html.erb
<%= render 'shared/carousel', :suffix => '_header', :caption => "this is caption" %>
_carousel.html.erb
<% images = Dir.glob("app/assets/images/*#{suffix}*") %>
<% id = "carousel-images#{suffix}" %>
<div id=<%= id %> class="carousel slide" data-ride="carousel">
    <ol class="carousel-indicators">
        <% images.count.times do |index| %>
            <li data-target=<%= id %> data-slide-to=<%= index %> <%= 'class=active' if index == 0 %>></li>
        <% end %>
    </ol>
    <div class="carousel-inner" role="listbox">
        <% images.each_with_index do |image, index| %>
            <% classes = ['item'] %>
            <%= content_tag(:div, :class => (index == 0 ? classes << 'active' : classes)) do %>
                <%= image_tag(File.basename(image), :alt => 'header') %>
                <div class="carousel-caption">
                    <h3><%= caption %><h3>
                </div>
            <% end %>
        <% end %>
        <a class="left carousel-control" href=<%= "##{id}" %> role="button" data-slide="prev">
            <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
            <span class="sr-only">Previous</span>
        </a>
        <a class="right carousel-control" href=<%= "##{id}" %>  role="button" data-slide="next">
            <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
            <span class="sr-only">Next</span>
        </a>
    </div>
</div>
10
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?