Ruby on rails 与第三方js库有关的rails 4资产管道混淆
我有两个问题,但我猜他们得出的答案是一样的 它们都与静态图像在开发模式下显示而不是在生产模式下显示这一事实有关(heroku) 案例1: 在my model.rb中Ruby on rails 与第三方js库有关的rails 4资产管道混淆,ruby-on-rails,heroku,ruby-on-rails-4,asset-pipeline,Ruby On Rails,Heroku,Ruby On Rails 4,Asset Pipeline,我有两个问题,但我猜他们得出的答案是一样的 它们都与静态图像在开发模式下显示而不是在生产模式下显示这一事实有关(heroku) 案例1: 在my model.rb中 has_attached_file :avatar, styles: { thumb: '32x32#', medium: '512x512#' }, :default_url => ActionController::Base.helpers.asset_path('/assets/avatar_:styl
has_attached_file :avatar, styles: {
thumb: '32x32#',
medium: '512x512#'
},
:default_url => ActionController::Base.helpers.asset_path('/assets/avatar_:style.png') ## <- This line (#1)
已附加文件:头像、样式:{
拇指:“32x32#”,
中等:“512x512#”
},
:default_url=>ActionController::Base.helpers.asset_path('/assets/avatar:style.png')##既然您是新手,让我来描述一下它是如何为您工作的:
--
资产
当您部署Rails应用程序时,您的资产将成为已知的应用程序的一部分。在开发过程中,您的资产文件存储在app/assets/…
使用时,Rails将为您调用以下路径:
app/assets/…
供应商/资产/…
public/…
您遇到的问题是在开发过程中,您的所有资产都存在于app/assets
中,因此您可以不加区别地调用所有您想要的文件
--
生产
当你在生产模式下运行你的应用程序时,事情会变得有点棘手
首先,你不能再依赖你的资产处于app/assets
状态了-生产模式通常(不总是,但肯定是在Heroku上)意味着你将不得不使用你的资产:
在生产环境中,Rails通过
违约然后,预编译的副本将作为静态资产由
网络服务器。应用程序/资产中的文件从未直接提供给
生产
这意味着,如果您想在生产环境中加载资产,特别是在Heroku上,则需要正确使用asset\u路径
(主要是因为Rails将加载您的生产/编译资产)
--
修复
直接提到你的问题,我会这样做:
一,
二,
资产管道并不能让您的生活变得轻松-曲别针假定默认url
可以用所需的值替换:style
部分,但这不起作用,因为这显然也会改变文件名的指纹部分
这里最简单的方法是不使用这些图像的资产管道。实际上,您不必将资产管道用于任何事情,与javascript或css文件相比,图像的好处非常小
要执行此操作,请将图像移动到public/images
。然后,对于第一种情况,将默认url更改为
/images/avatar_:style.png
对于第二种情况,将路径设置为/images
如果您愿意,您可以让第二个案例与资产管道一起工作,比如
:javascript
$('.star').raty({
readOnly: true,
starHalf: '#{asset_path('star-half.png')}',
starOff: '#{asset_path('star-off.png')}',
starOn: '#{asset_path('star-on.png')}',
cancelOn: '#{asset_path('cancel-on.png')}',
cancelOff: '#{asset_path('cancel-off.png')}',
noRatedMsg : "I haven't been rated yet!",
score: function() {
return $(this).attr('data-score');
}
});
谢谢你的回复。在实施您建议的更改时,我看到以下错误:案例1-未定义的方法
asset_path',对于#`和案例2
图像未显示,在firebug上看到以下错误-引用错误:未定义asset_url
,因此实际上这不起作用:(还有其他想法吗?谢谢@Frederick,成功了。我对此失去了希望。)
:javascript
$('.star').raty({
path: asset_url, #-> will use the asset_url
readOnly: true,
noRatedMsg : "I haven't been rated yet!",
score: function() {
return $(this).attr('data-score');
}
});
/images/avatar_:style.png
:javascript
$('.star').raty({
readOnly: true,
starHalf: '#{asset_path('star-half.png')}',
starOff: '#{asset_path('star-off.png')}',
starOn: '#{asset_path('star-on.png')}',
cancelOn: '#{asset_path('cancel-on.png')}',
cancelOff: '#{asset_path('cancel-off.png')}',
noRatedMsg : "I haven't been rated yet!",
score: function() {
return $(this).attr('data-score');
}
});