2014-06-16 52 views
0

所以我有一個應用程序在生產,我有一個評級系統與不同的明星形象,用戶可以點擊評級電影。在Rails上爲JavaScript中的圖像使用資產管道?

現在的圖像無法正常在瀏覽器中加載時,我檢查元素,它看起來是這樣的:

<img src="/assets/star-off.png" alt="1" title="not rated yet"> 

但圖像本身不顯示,當我在它懸停,它說:「失敗以加載給定的網址我不確定圖片是否應該上傳到S3或發生了什麼..有網站上的其他圖像有類似的視圖路徑,加載就好..

以下是代碼星形圖像存儲在哪裏(star-off.png,star-half.png等)

資產/ JavaScript的/ rateit.js.erb

$.fn.raty.defaults = { 
     cancel   : false, 
     cancelHint  : 'cancel this rating!', 
     cancelOff  : 'cancel-off.png', 
     cancelOn  : 'cancel-on.png', 
     cancelPlace  : 'left', 
     click   : undefined, 
     half   : false, 
     halfShow  : true, 
     hints   : ['bad', 'poor', 'regular', 'good', 'gorgeous'], 
     iconRange  : undefined, 
     mouseover  : undefined, 
     noRatedMsg  : 'not rated yet', 
     number   : 5, 
     path   : 'img/', 
     precision  : false, 
     round   : { down: .25, full: .6, up: .76 }, 
     readOnly  : false, 
     score   : undefined, 
     scoreName  : 'score', 
     single   : false, 
     size   : 16, 
     space   : true, 
     starHalf  : 'star-half.png', 
     starOff   : 'star-off.png', 
     starOn   : 'star-on.png', 
     target   : undefined, 
     targetFormat : '{score}', 
     targetKeep  : false, 
     targetText  : '', 
     targetType  : 'hint', 
     width   : undefined 
    }; 
現在

如果我把它放在一個資產路徑,如:

  starHalf  : <% asset_path('star-half.png') %>, 
      starOff   : <% asset_path('star-off.png') %>, 
      starOn   : <% asset_path('star-on.png') %>, 

在瀏覽器這個返回此:

<img src="/assets//assets/star-off-8fc67a767a197757fa4b6033012cd122.png" alt="1" title="not rated yet"> with (failed to load the given url when I hover over the broken image) 

我如何得到這個工作?我甚至試過<%image_tag('star-half.png')%>沒有運氣。

請幫忙!

+0

地段上SO信息的.. http://stackoverflow.com/questions/7381041/url-of-images-in-javascript-code-using-rails-3- 1-asset-pipeline/7383571#7383571 – Upperstage

+0

感謝上級,我確信會這樣做,我遵循這些交互並將asset_path存儲在var中。但它仍然返回相同的路徑與資產// assets/star-off ..等.. – user3399101

+0

更改您的問題的標題,包括RoR和資產預編譯,並刪除jQuery - 也許比我更有經驗的RoR開發人員將回答。我使用上面的鏈接在我的JS和CSS中使用asset_path。 – Upperstage

回答

0

通常你所描述的應該是工作;我做我認爲你一直在描述的東西,它的工作原理是,<%= asset_path('something.png') %>將JavaScript嵌入到應用圖片或其他資源中的正確方法。

所以你有這個部分是正確的,但其他東西一定會出錯。你在那裏有很多不同的部分。雖然你應該能夠將raty整合到Rails資產管道中,就像你正在嘗試的那樣,但有很多膠水層可以正確使用,如果你不明白髮生了什麼,你可能會滑落。

因此,您可能會發現有人爲您設置了一個試圖爲您製作Rails膠水的創意產品,幫助您做到了這一點。我還沒有嘗試過自己(或曾經使用過raty),但:

https://github.com/bmc/jquery-raty-rails

1

你的所作所爲是正確的。另一部分需要更改的是撥打raty,以刪除路徑配置。

例如:

$('.readonly-stars').raty({ 
    score: function() { 
     return $(this).attr('data-score'); 
    }, 
    half: true, 
    path: '/assets/', <-- remove this config, it's the source the second assets 
    readOnly: true, 
    hints: ['1', '2', '3', '4', '5'] 
}); 
相關問題