简体   繁体   中英

Image location when loaded by javascript in a rails project

I have a javascript plugin in my rails project that loads an image this way:

    $('.datepicker').after('<img class="datepickericon" src="images/icons/calendar.png" alt="calendar" />');

Unfortunately in a rails project this image location will point to a path relative to the view where the javascript is loaded. So the pointed location is something like

"admin/images/icons/calendar.png"

I know i could change the value of src to something like

"../public/images/[etc]/calendar.png"

This would work, but it is dependent on the amount of namespaces etc. I don't like to change the code of a plugin every time a use it for different situations.

So what i am looking for is a lasting solution to this image/file location problem when loading something in a (rails) project through javascript. Ofc, no absolute paths or ruby code in my javascript files.

You have a relative url. You want:

src="/images/icons/calendar.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