0

I have a method inside a Model that returns an image URL as a String such as: "assets/myImage.png", this works well in production without CDN, the image is being served.

Using the CDN (Cloudfront) serves only files with its fingerprint or that's what I've read. So when I open up my Google Console it shows "assets/myImage.png", but not the fingerprinted version, so it obviously doesn't show the image.

In short: I need to know how to use a helper method that returns the fingerprinted version of my image inside a Model.

Any help would be great!

Etheryte
  • 24,589
  • 11
  • 71
  • 116
gracegimon
  • 35
  • 6

3 Answers3

1

I believe you can access the Sprockets helper using:

  • self.class.helpers.asset_path('application.css') or
  • self.class.helpers.asset_digest_path('application.css')

Both of these are a backward way of getting access to the helpers that Sprockets uses for fingerprinting assets in production. Does that work?

Based on this SO answer

Community
  • 1
  • 1
gregb
  • 732
  • 5
  • 9
  • Hey! I finally got it working, thanks for your answer!! look: ActionController::Base.helpers.asset_path('') WORKED! :D that syntax you gave me, I'm not quite sure why it doesn't work! But thanks – gracegimon Jun 06 '14 at 01:57
  • So happy to help! This is my first SO answer :) Good luck @gracegimon! – gregb Jun 06 '14 at 20:50
0

You should be able to get the URL from the ruby aws-sdk gem.

Here are the docs for AWS::CloudFront::Client

Update: This answer is wrong. See my different answer below.

gregb
  • 732
  • 5
  • 9
0

I actually don't think my above answer will work. Instead, try this instide production.rb

config.action_controller.asset_host = "d24xjtg100euk4.cloudfront.net"

That should append the CDN url to all your rails helpers (like image_helper).

You could also roll your own custom helper that takes the asset location and concatenates it with the CDN url.

Based on this blog post: SETTING UP A CLOUDFRONT CDN FOR RAILS

EDIT: Posted another answer below using Sprockets helper method.

gregb
  • 732
  • 5
  • 9
  • Yes indeed I already set up that at prodcution.rb. All images are displaying ok except the one that i'm trying to return from a method in a model. – gracegimon Jun 05 '14 at 21:49
  • My method is as follows: `def myImage image_path("myImage.png") end` When I open up developer tools' network tab... it states that the image that the CDN tried to serve was myImage.png, and of course this doesn't exist in my CDN.. the one that exists is somethin 'myImage-.png' – gracegimon Jun 05 '14 at 21:51
  • the fingerprint is from the Rails asset pipeline correct? – gregb Jun 05 '14 at 22:07
  • I think I found the answer. See below. – gregb Jun 05 '14 at 23:57