Rails referencing assets in models Rails 4 and CDN - ruby-on-rails-4

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!

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

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.

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.

Related

How to prevent Ember.js 1.0.0 from removing URL parameters?

I use Ember 1.0.0 and i can not update to new versions now. I need a solution for Ember 1.0.0.
For example Google Adwords needs to pass custom parameters to visited website so Analytics can measure the traffic. I need to have it.
So for example URL is: mywebsite.com/someemberpage/?utm_source=test&utm_medium=test&utm_term=test&utm_content=test&utm_campaign=test
But after website loads Ember strips custom parameter. How to prevent it without updating Ember.js?
( In case Ember does not strip custom parameters please write, because if so, some JS code on my website does this. Thanks! )
Deprecated but these might be what your looking for...
https://github.com/knownasilya/ember-query-params
https://github.com/alexspeller/ember-query
In the past we used ember-query-params. It did the job just fine.
Hope this helps,
Jeff

Emberjs remove the hashtag from the URL

Its a quick question but I was wondering if it was possible to remove the # symbole from the URL in emberjs ? I know its not possible in many framwork, but I was curious to know about ember.
And if yes, how is it possible ?
If I understand it right removing the hash would defeat the whole point of using ember.js as other urls would point to other html pages and when you use ember.js you download only one page.
Yes you can see the relevant part in the Ember Guide
Keep in mind that doing so would mean that you need to configure your server to serve the ember app for all the possible routes that you can accept
it is also possible for you to make it so that the page that's served with such a route would have the added benefit of a normal emberless page in it's noscript tag as a form of SEO optimization (trying to reason why you would do this)
more can be found in the API Docs
revealing the nice option of using location:auto instead of location:history providing a more backwards compatible solution
App.Router.reopen({
location: 'auto'
});

Accessing images on production, from javascript, in Rails 4

It appears that now in Rails 4 using asset pipeline and the sprocket-rails gem, when images are processed, their filename is appended with an md5 fingerprint like css and javascript. While this makes sense because md5 fingerprints are awesome, it makes it increasingly difficult to access that image from javascript. In rails 3.2, I could access the image with /assets/image_name.jpg and it would serve properly, but in rails 4 that asset doesn't exist, it only exists with the md5 fingerprint in the name.
I know that rails provides helpers to access the image via erb <%= asset-url("image_name.jpg") %> but that is less ideal in javascript, because I am not using erb in my js. There are plenty of ways I could hack this with data-attributes serving in the views or using a script tag in my view and setting some globals, but I am looking for a nice solution to this problem, if it exists.
Any help is appreciated, thanks.
Another option to consider (although I wouldn't recommend it) is to use a custom route in your application controller to grab the asset path for you in the controller and either return the url to the asset with the md5 hash or possibly just render the raw binary data of the asset (although this will add processing overhead to your application).
For example, you make a AJAX get request to
http://yourapp.com/images?file=my_image.jpg
Then in your controller your action method would look like this:
def images
ActionController::Base.helpers.asset_url(params[:file])
end
This would then return the url path to the asset. The downside to this method is that it requires that you make two requests on the JS side. The first to get the path to the asset and the second to actually load that asset with the returned path.
To reduce this down to one request you could have the application read the image from the file system and return the proper headers so the browser thinks it is an image being returned and therefor will render the url provided. However, this would be a lot more work for the application and a lot more unneeded disk IO on your server.
It may take two requests for each image on the client to achieve what you want but you have to sacrifice somewhere...
Why do you need to use the asset pipeline for images? I get the hashing behavior. But normally the assets would be preprocessed. If you put the images in the public hierarchy as in olden times, you would get normal path routing.
Here's a quote from the Asset Pipleline guide that I think might be germane.
"Assets can still be placed in the public hierarchy. Any assets under public will be served as static files by the application or web server. You should use app/assets for files that must undergo some pre-processing before they are served."
Unfortunately, I think that you are stuck either adding an ERB extension to your JS and using the asset helpers, or else not using the asset pipeline for the assets.
When you say "I am not using erb in my js", do you mean you don't want to, or simply that you aren't? Because you can!
If you rename the relevant JS files with the extension .js.erb then you can use the asset_url helper in these files like so:
var src = "<%= asset_url('photo.jpg') %>";

keep media in base URL

So, I am looking for a solution to have images through the base website URL.
Example of image url:
www.website.com/image.jpg
Where the homepage lives at the url:
www.website.com/
I am using the django python web framework, and am aware this is a peculiar, if not just bad idea, but my client has linked several hundred images to their base URL in the above format, and refuses to change the existing links.
I know this is a vague question, but I can't seem to find anything relevant. Do I just set the media url to the same as the website?
Why don't you just configure your web server to rewrite the paths?
You could also configure it using an alternate host, assets.example.com for example and serve everything from there. Then you could rewrite all requests going at www.website.com to assets.example.com.

Some basic questions about Django, Pyjamas and Clean URLs

I am farily new to the topic, but I am trying to combine both Django and Pyjamas. What would be the smart way to combine the two? I am not asking about communication, but rather about the logical part.
Should I just put all the Pyjamas generated JS in the base of the domain, say http://www.mysite.com/something and setup Django on a subdirectory, or even subdomain, so all the JSON calls will go for http://something.mysite.com/something ?
As far as I understand now in such combination theres not much point to create views in Django?
Is there some solution for clean urls in Pyjamas, or that should be solved on some other level? How? Is it a standard way to pass some arguments as GET parameteres in a clean url while calling a Pyjamas generated JS?
You should take a look at the good Django With Pyjamas Howto.
I've managed to get the following to work, but it's not ideal. Full disclosure: I haven't figured out how to use the django's template system to get stuff into the pyjamas UI elements, and I have not confirmed that this setup works with django's authentication system. The only thing I've confirmed is that this gets the pyjamas-generated page to show up. Here's what I did.
Put the main .html file generated by pyjamas in django's "templates" directory and serve it from your project the way you'd serve any other template.
Put everything else in django's "static" files directory.
Make the following changes to the main .html file generated by pyjamas: in the head section find the meta element with name="pygwt:module" and change the content="..." attribute to content="/static/..." where "/static/" is the static page URL path you've configured in django; in the body section find the script element with src="bootstrap.js" and replace the attribute with src="/static/bootstrap.js".
You need to make these edits manually each time you regenerate the files with pyjamas. There appears to be no way to tell pyjamas to use a specific URL prefix when generating together its output. Oh well, pyjamas' coolness makes up for a lot.
acid, I'm not sure this is as much an answer as you would hope but I've been looking for the same answers as you have.
As far as I can see the most practical way to do it is with an Apache server serving Pyjamas output and Django being used as simply a service API for JSONrpc calls and such.
On a side note I am starting to wonder if Django is even the best option for this considering using it simply for this feature is not utilizing most of it's functionality.
The issue so far as I have found with using Django to serve Pyjamas output as Django Views/Templates is that Pyjamas loads as such
Main html page loads "bootstrap.js" and depending on the browser used bootstrap.js will load the appropriate app page. Even if you appropriately setup the static file links using the Django templating language to reference and load "bootstrap.js", I can't seem to do the same for bootstrap.js referencing each individual app page.
This leaves me sad since I do so love the "cruftless URLS" feature of Django.