【问题标题】:Bootstrap Carousel not moving as part of a Rails AppBootstrap Carousel 没有作为 Rails 应用程序的一部分移动
【发布时间】:2018-02-16 15:35:54
【问题描述】:

我想在我的 Rails 应用程序中使用 Bootstrap 轮播。 JQuery、Bootstrap 和 JS 都已加载并设置了工作数据库。所以我想知道我的轮播代码是否有错误。第一张图片显示,但没有改变,点击箭头不会移动到下一张。

<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">
    <% @products.each do |product| %>
	    <div class="item<%= " active" if product == @products.first %>">
	      <%= image_tag(product.image_url) %>
	      <div class="carousel-caption">
	        <%= product.name %>
	      </div>
	    </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>

【问题讨论】:

  • 您是否尝试在加载引导文件后将引导轮播初始化程序包含在 $(document).on('turbolinks:load', function() { ... } 中?

标签: javascript jquery ruby-on-rails twitter-bootstrap


【解决方案1】:

你的 JS 没有加载,为什么它不工作

确保在 application.js 中包含 bootstrap js 或仅将其包含在布局中

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>

【讨论】:

    【解决方案2】:

    好的,只是为了确保这是我加载引导程序的地方,到目前为止它没有引起任何问题,但到目前为止我只将它用于导航栏。

    <head>
        <title>Rails1</title>
        <%= csrf_meta_tags %>
    
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
        <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
    
        <%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
        <script scr="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js">
        </script>
        <meta name="viewport" content="width=device-width, initial-scale=1">
    
      
      </head>

    【讨论】:

      【解决方案3】:

      没有什么是必然的错误,但我之前使用的有效方法采用了稍微不同的方法,如下所示:

      <% @products.each_with_index do |product, index| %>
          <div class="item <%= 'active' if index == 0 %>">
            <%= image_tag(product.image_url) %>
            <div class="carousel-caption">
              <%= product.name %>
            </div>
          </div>
      <% end %>
      

      另外,您是否仔细检查过是否加载了 Bootstrap JS 文件?

      编辑:基于 JS 仍然无法运行的事实,我认为资产管道正确加载 JS 资产可能存在问题。虽然您可以通过 CDN 包含引导程序,但我建议您将:gem 'bootstrap-sass' 添加到您的 gem 文件和 bundle install。这将通过 Gem 添加引导程序(它更像是一种 Rails 方法),然后您将进入您的 application.js 文件并更新加载顺序,使其看起来像这样(假设您有 jquery_ujs):

      //= require jquery
      //= require bootstrap
      //= require jquery_ujs
      //= require turbolinks
      //= require_tree .
      

      【讨论】:

      • 刚刚发布了我加载 Bootstrap 的地方,只是为了确定。你的代码对我更有效,现在甚至没有显示第一个产品的图像。
      猜你喜欢
      • 1970-01-01
      • 2011-02-15
      • 1970-01-01
      • 2017-03-21
      • 2019-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多