Sauron
Sauron

Reputation: 6647

How to access images from CoffeeScript

I am attempting to access the image: app/assets/images/rails/png, in my CoffeeScript file but using

<img src='assets/images/rails.png' width='30' height='30'>

yields the console error:

enter image description here

How can I access the images stored in my images file from CoffeeScript?

Upvotes: 2

Views: 1513

Answers (1)

Surya
Surya

Reputation: 15992

Change your CoffeeScript file's extension from .js.coffee to .js.coffee.erb. After that you can do this(wherever you want to use images that are present in your assets/images directory:

'<%= asset_path 'rails.png' %>'

Note the single-quote: ', it is important because URL would be: /assets/rails.png and you would want it to present as a string. This saves you time of figuring out why you're getting some weird JavaScript undefined constant error.

Upvotes: 5

Related Questions