What is the purpose of using rails' image_url helper? - ruby-on-rails

From the docs it seems like you just save yourself from typing in the assets folder part of the image url when using this method against the vanilla url helper? What is the usefulness of this?

When you use image_url helper then rails will map the full qualified url based on the asset_host configuration. So this method gives you the freedom of just passing the name of the image as an argument and it does the rest.
Check this for more information.
http://api.rubyonrails.org/classes/ActionView/Helpers/AssetUrlHelper.html

Related

Ruby On Rails - Use "Format" As A URL GET Parameter?

I have a search page where I update the URL params on the page as filters are added or removed by the user. This allows me to deep link into the page (ie. going to /search?location=new+york&time=afternoon will set the location and afternoon filters).
I also have a filter named format. I noticed that passing in ?format=whatevervalue to the URL and then reloading the page with that param causes Rails to return a Completed 406 Not Acceptable error. It seems that format is a reserved Rails URL parameter.
Is there anyway to unreserve this parameter name for a particular endpoint?
In the context of an URL in Ruby on Rails there are at least four reserved parameter names: controller, method, id, format.
You cannot use these keys for anything else than for their intended purpose.
If you try to you will override the value internally set by Rails. In your example by setting ?format=whatevervalue you override the default format (html) and your application will try to find and render a whatevervalue template instead of the html formatted template. This will obviously not work.
Fun fact: Instead of using the default Rails path format like /users/123/edit you could use query parameters instead like this: /?controller=users&id=123&method=edit&format&html.
My suggestion is: Do not try to fight Rails conventions. Whenever you try to work around basic Rails conventions it will hurt you later on because it makes updates more difficult, common gems might break, unexpected side-effects will happen. Just use another name for that parameter.

Get fullpath from paperclip

I have paperclip, but its url method returns only the path not the full url. I want to construct the full url from that paperclip. Beside doing string concatenation, is there a better way to create full url from a image path string like "/images/11/1.jpg"?
E.g. Given the string "/images/11/1.jpg", I want to get "http://www.example.com/images/11/1.jpg"
Additionally, I want to switch the subdomain to 'assets'.
You can try to use environment variables in paperclip...
Using the Rails Enviroment URL in a MODEL with paperclip
but i don't know if it still works

Get ASP.NET bundle URL for use with CDN?

I'm using the ASP.NET bundling feature and want to know how I can get the URL returned by the Render helpers such as Scripts.Render("~/bundles/scripts").
Currently the optimized output is has a relative URL. I want to use a CDN that does origin-caching, so the final URL needs to be something like http://static.mydomain.com/bundles/scripts?v=XXXXXX
My plan is to simply write my own helper method, but I can't figure out how to get the version number relative URL for a given bundle.
The solution was rather simple. There is a static method Scripts.Url and Styles.Url that give me exactly what I want. I was able to incorporate this into my own helper to concatenate the CDN's base URL.

Paperclip Rails url and asset_host

According to the Paperclip S3 Docs one can specify the :url option in the config which has four possible values. There are comments related to this options that say:
The fourth option for the S3 url is :asset_host, which uses Rails' built-in asset_host settings.
To get the full url from a paperclip'd object, use the image_path helper; this is what image_tag uses to generate the url for an img tag.
These two comments seem in conflict with each other (to me). If Paperclip can use the asset_host settings, it seems almost necessary that it would generate the full url (since the asset_host only specifies the start (host) of that url)
But it then goes on to say you need to use a helper to get the full url??
The reason I ask this is because I want full urls generated for image url serialization (ie if we're returning json with image_urls, we want those served from our CDN).
Right now I've created a helper module that extends extend ::Sprockets::Helpers::RailsHelper to manually generate full urls any time an image_url is being serialized, but it's manual (and someone could possibly forget to do it in the future)
Any thoughts?

Access public files in Rails

I want to access the file /public/images/dog.jpg in the Rails project. Is there some URL route that would access it?
I tried public/images/dog.jpg and images/dog.jpg, but it didn't work.
images/dog.jpg is correct.
However I would use the image_path URL route, e.g. image_path('dog.jpg').
See http://guides.rubyonrails.org/asset_pipeline.html#coding-links-to-assets

Resources