carrier wave not loading default image url? - ruby-on-rails

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

Related

How to use svg 'use' statement with Rails sprockets asset helpers?

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.

Image src is not working in Rails

My haml file:
%img{:class => 'thumbnail', :src => '/assets/images/filename.jpg'}
It outputs correctly in the html browser:
<img class="thumbnail" src="/assets/images/filename.jpg">
But the image doesn't show up.
This is what Chrome is looking for:
http://127.0.0.1:3000/assets/images/filename.jpg
But when I try to visit that url, I'm being shown:
Routing Error
No route matches [GET] "/assets/images/filename.jpg"
Try running rake routes for more information on available routes.shown
Am I supposed to add something in the config file?? Any help would be much appreciated, thanks.
PS: I am working in development environment, so I serve my own static files.
You should use /assets/filename.jpg without images namespace.
You should be able to do = image_tag('filename.jpg', class: 'thumbnail') in your haml file.
Also, you can check if there's any image before adding the path by just going to the browser and see if anything comes up, like:
http://127.0.0.1:3000/assets/images/filename.jpg
http://127.0.0.1:3000/assets/filename.jpg
I solved my issue. Because I am using Rails 3.2 and this version uses the asset pipeline, you have to declare the directories you want to be public. You do this by adding this line in the config/application.rb file:
config.assets.paths << Rails.root.join("path", "to", "dir", "from", "root")
In my case, I wanted the medias/images/thumbnails dir to be part of the assets (medias is at root level), so I achieved this simply by adding:
config.assets.paths << Rails.root.join("medias", "images", "thumbnails")
I can then access them in the templates like other assets with src="/assets/filename.jpg". This is practical because in my case I just want the thumbnails to be public, not the original pictures.

HTML <img> tag with Heroku CDN?

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.

How to rewrite Rails assets path?

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!

Rails 3: On a local server (# rails server), my images weren't found, although the correct location is given

I started the rails server with rails server.
My images are in public/images, i.e. the image public/images/rails.png is put into html with <img src="/images/rails.png" /> just as the image_tag helper generates it.
But by any reason, the picture isn't found. If I enter the source of the file directly into the browser, I get a routing error: "No route matches http://0.0.0.0:3000/images/rails.png".
Any help?
Yours,
Joern
Check your environments' config file, ie. config/environments/production.rb. If there's a setting config.serve_static_assets = false , the problem you described will occur.

Resources