With Asset Pipeline how to serve Images in Javascript?

I have the following js in my assets/javascript

jQuery ->
  if $('.pagination').length
    $(window).scroll ->
      url = $('.pagination .next_page').attr('href')
      if url && $(window).scrollTop() > $(document).height() - $(window).height() - 50
        $('.pagination').html('<img src="images/spinner.gif" />')
        $.getScript(url)
    $(window).scroll()

Here I am calling for a spinner animation, but is not fetched on localhost.

No route matches [GET] "/images/spinner.gif"

I have already ran

rake assets:precompile:all RAILS_ENV=development

and my image is properly placed in assets/images (and in public) So this makes me think that the way I call for this image must be wrong.

Answers


First of all have a read at rails asset pipeline and read difference between developement and production way of serving assets this will help you understand how the assets are served

If you add an erb extension to a JavaScript asset, making it something such as application.js.erb, then you can use the asset_path helper in your JavaScript code:

   jQuery ->
  if $('.pagination').length
    $(window).scroll ->
      url = $('.pagination .next_page').attr('href')
      if url && $(window).scrollTop() > $(document).height() - $(window).height() - 50
        $('.pagination').html('<%= asset_path('logo.png') %>')
        $.getScript(url)
    $(window).scroll()

Need Your Help

MS Access VBA loop through list

vba ms-access

I think this should be simple, but I can't find the right way to do it. I have a couple examples of the situation I'm trying to resolve, so I'll describe both. I think they likely can be handled ...

open file for each element in array and check against a regex Perl

regex arrays perl io

I have an array filled with 4 digit numbers (@nums) that correspond

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.