How to use images on the error pages(404, 500 ...)? - ruby-on-rails

How to use images on the error pages(404, 500 ...)?
Can I use images from a folder "assets" ?
I need to compile them or no?

You can create an image folder within the public folder, in the root of your application, and then from there, you can access them by referencing it directly.
An example on the 404 html file.
<div class="dialog">
<div>
<h1>The page you were looking for doesn't exist.</h1>
<p>You may have mistyped the address or the page may have moved.</p>
</div>
<p>If you are the application owner check the logs for more information.</p>
<img src="images/01.jpg" alt="">
</div>
public/images/ contains the 01.jpg image file.

Another idea that you could consider is having the 404.html and 500.html located on Amazon S3. Something like that would accomplish you having your picture(s) available on S3 with the entire page itself.
Then if you are using a service like Heroku to host your application, you can change the ERROR_PAGE_URL variable to point to your error page on S3.

If you can't or don't want to store the images anywhere else, you can use Base64 encoding to have the images inside the HTML code of the error page itself. You can use any Base64 encoder such as https://base64.guru/converter/encode/image

Related

How to create a direct link to an image for other applications to access in Rails 5.2

I am customizing the checkout page on PayPal.
To provide a logo and header image they require a URL that links directly to the image of the logo or header image such as https://some.domain.name/logo.png. I have to enter this URL on PayPal's website so that it can grab the image from my website to put on their checkout form.
I've tried placing the logo under the public directory and providing the URL: https://some.domain.name/logo.png
But this just gives a no route error.
How can I do this in Rails?
You need to put your images in your app/assets/images folder because Rails uses asset pipeline. Then you can use image_url helper to get a full url, ex:
ActionController::Base.helpers.image_url("logo.png")
I don't know what do you mean by direct link the rails way provided the functionalities for get the image link like
image_tag("rails.png")
# => <img alt="Rails" src="http://some.example.com/assets/rails.png" />
put the image on the app/assets/
also, put the direct static way like
<img alt="Rails" src="app/assets/rails.png" />
Here the doc
Hope it helps

Images not rendering in localhost

I have images sitting within my project root at Data\Images\articles. I am trying to render the image using the code below in MVC:
<img src="#Url.Content(Model.Image)" alt="Image" />
It's not displaying the image, however the rendered html code is correct.
<img src="/Data/Images/Articles/16fd7bc5-3bac-474a-a806-b8d72b974960.PNG" alt="Image">
I am running the project from Visual studio server, not through IIS. Any idea what I am doing wrong here?
Different file name was generating as compare to what was expecting in logic causing 404 error when validating the image file name

linking to html file in /public

I have a file 'maps.html' located in /public. I am loading up an index page with contents as follows:
<%= link_to 'redirect_click_here', 'maps.html' %>.
Application.html.erb takes care of the other necessary html elements for the page.
The result of clicking that link is that I am sent to /map/maps.html.
This is slightly logical: the page hosting the link was in the 'map' controller. Still, I want to 'escape' the controller and access the public html file.
I realize that this is a kind of pointless request because I could just put the html file in app/views, but it's just for completion's sake that I put forth this request.
edit
One reason I want to include this file from the /public/ directory is that I don't want it to go through the asset pipeline and inherit the html document structure from application.html.erb. I am going to be including HTML files which include custom heads and I don't want to have to replace the contents of application.html.erb every time.
You should use '/maps.html' in your link, so that it knows it is in the root public folder.
You can access it by
<%= link_to "Maps", "/your_project/maps.html" %>

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.

PDFkit doesn't display pictures in PDF

Rails 2, PDFkit 0.5.0
Im generating a PDF from a View in Rails 2 with PDFkit and everything works fine. The only thing which doesn't work is displaying pictures in the pdf.
When I look at the View in the Browser, the picture is there but its missing in the PDF. There is only a placeholder existing in the PDF.
The image_tag is looking like this:
<%= image_tag('plus.gif') %>
I also tried to realize it with a css-file but it doesn't work either.
Any ideas?
Because of the way that wkhtmltopdf works you need to specify the full path to any assets (JS, CSS, images etc), including the domain name.
This won't work:
<img src="/images/foo.png" />
This will:
<img src="http://example.com/images/foo.png" />
One workaround is to set an explicit asset host, even if it's the same server as your app is running on (see the AssetTagHelper documentation for details). Another would be to specify the hostname in the image_tag.
Instead of putting the full path each time, you can add a base tag to the head section.
<base href="http://mydomain.com" target="_blank" />

Resources