0

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.

1 Answer 1

1

You have a relative url. You want:

src="/images/icons/calendar.png"
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I didn't knew that a forward slash in the beginning of a url replaces the base url, i.e. '/' replaces 'localhost:####'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.