【问题标题】:Dynamically Adding images to Bootstrap Carousel in Rails 4 app在 Rails 4 应用程序中将图像动态添加到 Bootstrap Carousel
【发布时间】:2017-03-21 23:31:00
【问题描述】:

我正在使用 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>

【问题讨论】:

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


【解决方案1】:

您可能会缺少“活动”类或将“活动”类添加到所有项目。将“活动”类添加到您要首先显示的照片中。

下面的代码会将“active”类添加到第一张图片。

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

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

【讨论】:

  • 谢谢,但它不起作用,我在JS console 中收到此错误:carousel.self-e47323f….js?body=1:148 Uncaught TypeError: Cannot read property 'offsetWidth' of undefined(…)
  • 能否请您检查一下url是否对您有帮助?
  • 我不确定它是否有帮助,网址中的答案建议调用active,但这就是循环正在做的事情。也许url中的其他一些建议可以工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-30
  • 1970-01-01
  • 2018-02-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多