5

Rails 3.1 now requires you to use image_tag when rendering an image using the asset pipeline.

I have built endless scroll into my application and have put the code into a js.coffee file. I wish to render a spinning loading gif whilst more products are loaded. I cannot use image_tag here as this file does not support rails code, but I have written it in here so you understand what I am trying to do.

jQuery ->
  if $('.pagination').length
    $(window).scroll ->
      url = $('.pagination .next_page').attr('href')
      if url && $(window).scrollTop() > $(document).height() - $(window).height() - 1200
        $('.pagination').html("<%= image_tag("loading.gif") %> Loading more...")
        $.getScript(url)
    $(window).scroll()

Previously, I would have just written it in pure HTML, using <img src=... But this will no longer work with the asset pipeline. How can I achieve this?

1 Answer 1

5

Using plain HTML should work just fine.

Try using: <img src="/assets/loading.gif" /> if your loading.gif is inside assets/images.

UPDATED 21/06/2012

As per the Ruby on Rails Guide, Section 2.2.3, changing the file extension of your .js file to filename.js.erb or filename.js.coffee.erb will allow you to use embedded ruby inside your javascript.

You can then use the asset_path helper to access the path where your assets are stored.

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

7 Comments

Will this not only work in production if I include the fingerprint part? It would have to be something like <img src="/assets/loading-3ca5380af3c933ae32.gif" />
Hmm, good point. I hadn't thought of that, as I was only toying around with something on my dev environment. I'll take a look into it here shortly.
I mean I could put the image_tag in a div in the view with display none and then put $('.paginationLoader').show() in the js code. But this just seems unnecessary, and a bit of a work around, there must surely be a better way. Would be interested if you were able to come up with anything, thanks!
According to the documentation, you should be able to access the asset_path helper from your coffeescript file. Look here at section 2.2.3.
I just tried it and it didn't work initially. I'll try some tweaking when I'm home later, it does look promising. Should at least help know what terms to search for a little better. Thanks a lot.
|

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.