2017-08-14 70 views
14

實現JQuery進度條,因此當您向下滾動時,它應該在頂部顯示一個綠色條。當我開始滾動時,進度條沒有出現。我在酒吧檢查元素它顯示寬度%的元素往上看截圖Rails5 + JQuery進度條不起作用

enter image description here

enter image description here

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 + '%') 

}) 

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> 

看着日誌沒有找到scr OLL但它的顯示寬度元往上走,(見前頁圖)

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' 

產品controller.rb

def set_product 
     @product = Product.find(params[:id]) 
    end 

我有這個在我的路線

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' 

enter image description here

+0

是什麼觸發了這個請求'Started GET「/products/scroll.js」'?它與**足智多謀的show route **衝突,並且由於Rails在url中預期了':id',所以它將'scroll'視爲導致錯誤的'id'。 – Pavan

+0

在我的路線我有資源:產品https:// github。com/neilp666/product_pricing_calculator – Neil

+0

沒關係,但我想知道是什麼觸發了這個請求'Started GET「/products/scroll.js」'? – Pavan

回答

9

此錯誤

的ActiveRecord :: RecordNotFound

不正確的資源路徑觸發(無法與 'ID'=滾動找到產品)。當你看着app/layouts/application.html.erb,你會看到這個

<script src="scroll.js"></script> 

網址SRC是錯誤。然後這作爲/products/scroll.js/products/:id衝突並導致該錯誤。您在/app/assets/javascripts文件夾中有scroll.js,因此您需要使用/assets/scroll.js。改變它像下面應該修正這個錯誤

<script src="/assets/scroll.js"></script> 

OR

剛從application.js通過包括像這樣

//= require scroll 

產生

<script src="/assets/scroll.js?body=1"></script> 

叫它我建議你閱讀約Asset Pipeline更好理解。

+0

添加了代碼&它的工作原理,但只有在progress div類中添加一些文本時纔有效。如果我不添加任何文本,它不會顯示進度欄。但是,謝謝,我將再次檢查資產管道! – Neil