2014-06-16 53 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'] 
}); 
相关问题