简体   繁体   中英

Passing an image filename to JavaScript

I am developing a Rails v4.2 website.

I have a JavaScript object that takes the name of an image file as a property:

var obj = new objects.Factory.MyObject({
    title: 'Sir',
    education: 'Phd',
    icon: './profile.png'
  });

I placed my images in "app/assets/images", however when I load the page, I see a 404 error when trying to retrieve the image.

I tried using image_tag which obviously did not work in Javascript. How may I correctly specify the path of an image in a Javascript code section of a Rails page?

我将通过在扩展名的末尾附加.erb来重命名JavaScript文件,然后在JavaScript文件中的以下行中加载assets/images

<%= asset_path('profile.png') %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM