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