Kamilski81
Kamilski81

Reputation: 15107

Access asset_host inside javascript for Rails?

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)

Upvotes: 2

Views: 938

Answers (1)

Richard Peck
Richard Peck

Reputation: 76774

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

Upvotes: 1

Related Questions