I began moving all the assets in my site to S3 and ran into a problem with my asset path.
There's a WYSIWYG editor on my site that includes images by absolute path, so when you add an image, it doesn't use the rails image_tag helper but rather adds an image like this:
<img src="/system/images/image_1.jpg" />
The problem is that in production the URL /system/images/image_1.jpg leads to a non-existant file.
Naturally, two solutions are to 1) replace the URL dynamically (gsub) when it's called and 2) to loop through the database and replace the urls.
A better solution, however, would be to rewrite the /system/images/image_1.jpg url to point to S3. How do I do that?
Thanks!
Related
When I run rails in production mode , I put the default url of avatar in assets/images and made it like this :
"/assets/" + [version_name, "image.jpeg"].compact.join('_')
it works perfevtly in development mode but in production it doesn't and I don't know why also I have pre compiled assets but still so hope you help .
If the path of your asset is:
/assets/v1_image.png
Then it will perfectly work fine in development if you used something like:
<img src="/assets/v1_image.png" />
Because in development assets are not precompiled by Rails asset pipeline.
But in production the asset pipeline will compile your assets and stamp it with some md5hash so your file name will end-up to be like:
/assets/v1_image-aee4be71f1288037ae78b997df388332edfd246471b533dcedaa8f9fe156442b.png
So the correct way to use it should be something like:
<img src="/assets/v1_image-aee4be71f1288037ae78b997df388332edfd246471b533dcedaa8f9fe156442b.png" />
but still this md5 hash will change from a deployment to another so the rails way to handle this is to use image_path, image_url or image_tag helpers from ActionView::Helpers::AssetUrlHelper to generate the correct path for you as follows:
image_tag('v1_image.png') will return:
<img src="/assets/v1_image-aee4be71f1288037ae78b997df388332edfd246471b533dcedaa8f9fe156442b.png" />
image_path('v1_image.png') will return:
/assets/v1_image-aee4be71f1288037ae78b997df388332edfd246471b533dcedaa8f9fe156442b.png
image_url('v1_image.png') will return:
http://www.example.com/assets/v1_image-aee4be71f1288037ae78b997df388332edfd246471b533dcedaa8f9fe156442b.png
I hope this is clear enough and can fix your problem :)
The problem could be due to /assets interfering with rails asset pipeline.
Please post your code for a better understanding of the question
I have an svg in an external file that I want to reference with a use statement in Rails.
If I do:
%svg
%use{"xlink:href" => "assets/icon.svg#test"}
which generates the html:
<svg>
<use xlink:href="assets/icon.svg#test"></use>
</svg>
Everything works as expected.
However I want this to be able to work with sprockets asset versioning in a similar way to how image_tag works.
I tried to do:
%svg
%use{"xlink:href" => image_url("icon.svg#test")}
This generates the html:
<svg>
<use xlink:href="http://0.0.0.0:5000/assets/icon.svg#test"></use>
</svg>
The asset certainly exists at http://0.0.0.0:5000/assets/icon.svg, but the icon does not show.
What am I doing wrong? How do I use sprockets asset helpers with svg use statements?
Asset cannot exist at 0.0.0.0, it is not a real ip, you need to set config.action_controller.asset_host
In development 127.0.0.1(loopback ip) will do
Seems to be a cross origin browser security issue.
Google chrome actually gives a message that helps with this. (I was previously using firefox so didn't notice this...)
Unsafe attempt to load URL http://0.0.0.0:5000/assets/icon.svg from frame with URL http://localhost:5000/. Domains, protocols and ports must match.
The domain names much match for it to work.
In my opinion, this can be fixed by using image_path instead of image_url. That way you should get a relative link to the SVG file, i.e. precisely the same output as in your raw HTML, possibly with just the asset hash added to the file name.
How can I get the absolute URL of an image in Rails.
Rails serves the images using assets.
So an image by the name "logo.png" is actually stored as "logo-776bfb0d3b4bdea029da753cf63916e2.png".
This is not a problem when I need to access the image using a Rails server as I render it using the <%= image_tag %> helper.
But I need to pass the actual URL to an API which will then render the image.
So how can I just get the physical URL??
This is the name of the file. It will be in your public/assets directory and the url will be
your-site-name/assets/logo-776bfb0d3b4bdea029da753cf63916e2.png.
Note this will change when you precompile your assets and the file is given a new fingerprint. You will be much better off using the rails image_url helper to give you the URL and passing that to your API.
If you are using rails 4. Then the image will be in name_of_your_app/app/assets/images/.
You probably have to include ActionView::Helpers::AssetUrlHelper. Or you could just call ActionController::Base.helpers.image_tag("image.png")
I have a question about using asset_sync and a Heroku CDN. In this article, it says
Make sure you’re using the Rails AssetTagHelper methods (like
image_tag). This will ensure all of your assets will reference the new
asset host.
Does that mean any plain html <img> tags or refs in my app won't work? Or maybe it's just to warn against tags with an absolute URL?
EDIT: I know I can and should use image_tag and image_path in views or css. What I'm asking is, do I HAVE to?
They will work but you will need to point it manually to where you are syncing your assets to, some bucket on Amazon S3. Not really recommended unless your assets will hardly ever change.
You configure your asset path in your production.rb config like so:
config.action_controller.asset_host = "http://assets.domain.com"
Then whenever you reference asset_path it will point to the asset on the host defined in your environment config.
Perhaps a solution (without understanding your exact problem) would be to do something like this:
<img src="<%= asset_path("image.png") %>" />
You should to use
<%= image_path("logo.png") %>
instead of
<img src="<%= asset_path("image.png") %>" />
You can more details about this helper method here. Also As specified by andrew you need to specify asset_host in you config file. Here is a small blogpost for the same
Also:
If you want to pull css background icons/images from amazon s3 then use:
background-image: image_url("icon.png"); // it requires scss extension ie saas and also you must has saas rails gem included.
By default, rails is looking for images to "public/images" folder.
But it is not suiteable for me, since i have all the multimedia stuff in "public/data/:model/:id" folders.
How do i force rails to look into this folder.
I don't need obligatory such a pattern, that i mentioned above.
The only thing, i need, is just to change "public/images" path to "public/data", so, the rails should look to "data" folder, instead of "images".
I don't need to use "paperclip" plugin, because my models holding the pure HTML content (with clean and simple structure, that will not change in the future), that has "img" tags.
How can it be done ?
You you mean the image_tag helper is looking by default there? That's only the case if you only specify a relative path. Putting the full path in gets what you want I believe.
<%= image_tag '/data/model/1'
would generate
<img src="/data/model/1.png">