【发布时间】: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