2

I have the following javascript (shortened for clarity):

$modal = $('<img src="/assets/loading.gif">');
$('body').append($modal);

But I am not sure how to access the asset_host within that javascript? Any suggestions? (I am trying to not use coffee script)

1 Answer 1

1

There are several options:


asset_path

If you're not accessing the file inside the asset pipeline (and consequently can use ERB code), you could use this:

$img = '<%=j image_tag asset_path("loading.gif)" %>'
$('body').append($img);

Gon

If you are accessing inside the asset pipeline (where you can only use pure JS), you'll have to pass the asset host variable to JS through HTML. Gon is a great way to do this:

#config/initializers/gon.rb
gon.push({asset_host: ActionController::Base.asset_host})

#app/views/layouts/application.html.erb
<%= include_gon %>

#app/assets/javascripts/application.js
$modal = $('<img src="' + gon.asset_host + '/assets/loading.gif">');
$('body').append($modal);

Got the asset_host reference from here

Sign up to request clarification or add additional context in comments.

Comments

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.