【发布时间】:2014-03-08 02:16:04
【问题描述】:
如何在我的 application.js 中使用此代码,以便我可以预编译此处使用的图像?我应该使用什么 rails helper?
$(document).ready(function(){
$(".imgthumb").click(function(){
$(".thumbnailcurrent").addClass("thumbnails");
$(".thumbnails").removeClass("thumbnailcurrent");
$(this).addClass("thumbnailcurrent");
$(this).removeClass("thumbnails");
var img=($(this).attr("data-id"));
if (img==1) {
$('.imgbig').html("<%= asset_path src: 'flappy1.jpg', height: '275', width: '391' %>");
} else if (img==2) {
$('.imgbig').html("<%= asset_path src: 'flappy2.jpg', height: '275', width: '391' %>");
} else if (img==3) {
$('.imgbig').html("<%= asset_path src: 'flappy3.jpg', height: '275', width: '391' %>");
}
});
});
【问题讨论】:
-
当您说“预编译图像”时,您的意思是要从 JS 中预加载它们吗?
-
我希望上面的代码最终能正常工作。它存储在 applications.js.erb 文件中。你觉得它合适吗?不确定我是否正确设置了asset_path 和 src: 实现。第二双眼睛会很棒,非常感谢。
-
也许 $('.imgbig').html("");
-
嘿,我想我明白你的问题了。也许,这就是你需要的? github.com/kavkaz/js_assets
-
是的,基本上图像显示在开发中,而不是在生产中。那个 gem 能解决问题吗?谢谢!
标签: javascript jquery ruby-on-rails ruby-on-rails-3 ruby-on-rails-4