简体   繁体   中英

How to interpolate ruby and coffee script variable in `coffee.erb` file?

I want to use a function like this in app/assets/javascripts/foo.js.coffee.erb file.

loadImage = (size="normal") -> imgName = "<%= asset_path('load-#{size}.gif') %>"

This code doesn't work, because coffee script variable is not interpolated. If I write '<%= asset_path("load-#{size}.gif") %>' , then #{size} is recognized ruby variable and needless to say, it doesn't work neither.

How can I write a code like this?

In case someone will search for the same:

You can use coffee_script_tag for it.

loadImage = (size="normal") -> imgName = "<%= asset_path('load-#{coffee_script_tag(size)}.gif') %>"

and that should work.

It looks weird, so I suppose you would not need this until you are doing something wrong. At least one should remember that the erb-expression will be evaluated once, with the value when the page is rendered.

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