在Rails 4应用程序中动态添加图像到Bootstrap Carousel

时间:2016-11-08 11:03:33

标签: javascript ruby-on-rails twitter-bootstrap loops carousel

我正在使用Active Admin将图片上传到我的应用,这很正常。但我希望上传的图像显示在Bootstrap Carrousel中。我缺乏使用Rails和Javascript的经验限制了我得到我想要的结果。

Javascript控制台会抛出此错误:

carousel.self-e47323f….js?body=1:148 Uncaught TypeError: Cannot read property 'offsetWidth' of undefined(…)
  Carousel.slide @ carousel.self-e47323f….js?body=1:148
  Carousel.next @ carousel.self-e47323f….js?body=1:110
  (anonymous function) @ carousel.self-e47323f….js?body=1:186
  each @ jquery.self-bd7ddd3….js?body=1:371
  each @ jquery.self-bd7ddd3….js?body=1:138
  Plugin @ carousel.self-e47323f….js?body=1:178
  clickHandler @ carousel.self-e47323f….js?body=1:218
  dispatch @ jquery.self-bd7ddd3….js?body=1:5227
  elemData.handle @ jquery.self-bd7ddd3….js?body=1:4879

修改 我查看了可能的重复内容,我可以看到这可能是重复的,但我不确定如何将我的Ruby loop集成到javascript中。

有人可以告诉我。代码在

之下

已修改 已更新代码

<div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
<!-- Indicators -->
<ol class="carousel-indicators">
  <li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li>
  <li data-target="#carousel-example-generic" data-slide-to="1"></li>
  <li data-target="#carousel-example-generic" data-slide-to="2"></li>
</ol>

<!-- Wrapper for slides -->
  <div class="carousel-inner" role="listbox">

  <% @product.photos.each_with_index do |photo, index| %>                     
   <div class="item #{"active" if index.zero? }">
  <%= image_tag(photo.image.url(:large)) %>                    
   </div>
  <% end %>

   ...
  </div>

   <!-- Controls -->
   <a class="left carousel-control" href="#carousel-example-generic" 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="#carousel-example-generic" role="button" data-slide="next">
     <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"> </span>
     <span class="sr-only">Next</span>
   </a>
 </div>

 <script type="text/javascript">
   $('.carousel').carousel({
   interval: 2000
   })
 </script>

1 个答案:

答案 0 :(得分:0)

可能你会错过课程&#39;活跃的&#39;或者课程“活跃”被添加到所有项目。添加课程&#39;有效&#39;到你想先显示的照片。

下面的代码会添加“活跃”字样。到第一张图片。

<% @product.photos.each_with_index do |photo, index| %>                     
  <div class="item #{"active" if index.zero? }">
    <%= image_tag(photo.image.url(:large)) %>                    
  </div>
<% end %>

另外,请检查您网页上的javascript错误。