Rails5 + JQuery进度条不起作用

Nei*_*eil 13 html javascript css jquery ruby-on-rails

实现一个JQuery进度条,这样当你向下滚动时,它应该在顶部显示一个绿色条.当我开始滚动时,进度条不会出现.我在条形图上检查元素它显示的宽度%向上看截图

在此输入图像描述

在此输入图像描述

scroll.js

$(document).on('scroll', function() {

  var pixelsFromTop = $(document).scrollTop()

  var documentHeight = $(document).height()
  var windowHeight = $(window).height()

  var difference = documentHeight - windowHeight

  var percentage = 100 * pixelsFromTop / difference

  $('.bar').css('width', percentage + '%')

})
Run Code Online (Sandbox Code Playgroud)

show.html.erb

<div class="progress">
   <div class="bar"></div>
</div>

<section class="day">

    <h2><%= "To make #{number_to_currency @product.revenue}" %></h2>
    <p class="lead"><%= "You need to make #{number_to_currency @product.monthly_amount} a month" %><br/>
    <%= "You need to make #{number_to_currency @product.daily_amount} a day" %></p>
</section>


<section class="people"> 

    <h2>Or if you create and sell a product</h2>
        <table>
        <p class="lead"><%= "To make #{@product.revenue } 10,000 people to buy a
        #{number_to_currency @product.create_and_sell_product_10000} product" %><br/>

        <%= "To make #{@product.revenue } 5,000 people to buy a
        #{number_to_currency @product.create_and_sell_product_5000} product" %><br/>

        <%= "To make #{@product.revenue } 2,000 people to buy a
        #{number_to_currency @product.create_and_sell_product_2000} product" %><br/>

        <%= "To make #{@product.revenue } 1,000 people to buy a
     #{number_to_currency @product.create_and_sell_product_1000} product" %><br/>

        <%= "To make #{@product.revenue } 100 people to buy a
     #{number_to_currency @product.create_and_sell_product_100} product" %></p>
        </table>
</section>
Run Code Online (Sandbox Code Playgroud)

看着日志它没有找到卷轴,但它显示宽度元素上升,(见prev screenshot)

Started GET "/products/scroll.js" for 127.0.0.1 at 2017-08-14 12:27:17 +0100
Processing by ProductsController#show as JS
  Parameters: {"id"=>"scroll"}
  Product Load (0.4ms)  SELECT  "products".* FROM "products" WHERE "products"."id" = $1 LIMIT $2  [["id", 0], ["LIMIT", 1]]
Completed 404 Not Found in 3ms (ActiveRecord: 0.4ms)

ActiveRecord::RecordNotFound (Couldn't find Product with 'id'=scroll):

app/controllers/products_controller.rb:67:in `set_product'
Run Code Online (Sandbox Code Playgroud)

产品controller.rb

  def set_product
      @product = Product.find(params[:id])
    end
Run Code Online (Sandbox Code Playgroud)

我的路线中有这个

Rails.application.routes.draw do
  resources :products

  root 'products#new'
end


Started POST "/products" for 127.0.0.1 at 2017-08-16 17:03:11 +0100
Processing by ProductsController#create as HTML
  Parameters: {"utf8"=>"?", "authenticity_token"=>"ZU4tSNn0PsbSL2CTB45yIuBlrn5OePTYPSbIcdKThzJuu/k7GsiIhFf7JJ98pC5NmhGVg5QyekokpzR1s4tv4A==", "product"=>{"revenue"=>"2000", "months"=>"2"}, "commit"=>"Show Me How Much to Charge!"}
   (0.9ms)  BEGIN
  SQL (3.5ms)  INSERT INTO "products" ("revenue", "months", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id"  [["revenue", "2000.0"], ["months", 2], ["created_at", "2017-08-16 16:03:11.679460"], ["updated_at", "2017-08-16 16:03:11.679460"]]
   (46.6ms)  COMMIT
Redirected to http://localhost:3000/products/82
Completed 302 Found in 57ms (ActiveRecord: 51.0ms)


Started GET "/products/82" for 127.0.0.1 at 2017-08-16 17:03:11 +0100
Processing by ProductsController#show as HTML
  Parameters: {"id"=>"82"}
  Product Load (0.3ms)  SELECT  "products".* FROM "products" WHERE "products"."id" = $1 LIMIT $2  [["id", 82], ["LIMIT", 1]]
  Rendering products/show.html.erb within layouts/application
  Rendered products/show.html.erb within layouts/application (7.2ms)
Completed 200 OK in 398ms (Views: 395.0ms | ActiveRecord: 0.3ms)


Started GET "/products/scroll.js" for 127.0.0.1 at 2017-08-16 17:03:12 +0100
Processing by ProductsController#show as JS
  Parameters: {"id"=>"scroll"}
  Product Load (0.3ms)  SELECT  "products".* FROM "products" WHERE "products"."id" = $1 LIMIT $2  [["id", 0], ["LIMIT", 1]]
Completed 404 Not Found in 1ms (ActiveRecord: 0.3ms)



ActiveRecord::RecordNotFound (Couldn't find Product with 'id'=scroll):

app/controllers/products_controller.rb:67:in `set_product'
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

Pav*_*van 8

这个错误

ActiveRecord :: RecordNotFound(找不到'id'=滚动的产品)

不正确的资产路径触发.当你研究时app/layouts/application.html.erb,你会看到这一点

<script src="scroll.js"></script>
Run Code Online (Sandbox Code Playgroud)

srcurl是错误的.然后/products/scroll.js它与之冲突/products/:id并导致该错误.你必须scroll.js/app/assets/javascripts文件夹中,所以你需要使用/assets/scroll.js.将其更改为如下所示应修复错误

<script src="/assets/scroll.js"></script>
Run Code Online (Sandbox Code Playgroud)

要么

只需将它application.js包括在内即可

//= require scroll
Run Code Online (Sandbox Code Playgroud)

产生

<script src="/assets/scroll.js?body=1"></script>
Run Code Online (Sandbox Code Playgroud)

我建议你阅读资产管道以便更好地理解.