Generating Mustache Template With Asset Images in Rails - ruby-on-rails

I'm generating a Mustache template within a controller of my rails application. I am able to generate the html just fine. My problem is that I am not able to load in images from the assets folder.
I've tried using ActionController::Base.helpers.image_tag and ActionController::Base.helpers.asset_path and then passing that in as a variable into an image tag in the Mustache template as <img src="{{ asset_path }}" /> for the asset path and the output of the image_tag as it is <div> {{ image_tag }} </div>
I know that image_tag and asset_path are picking up the image because when I put in an incorrect path it outputs an error.
Anybody have any ideas on how to link this image to the Mustache template?

This issue has been resolved. The error was not related to Mustache but to the next phase of the application which is to generate a PDF from the generated HTML.
For reference, I used Rails.root.join('app','assets','..path to image') for the image path.

Related

the image does not appear in web browser using rails 5

I have problem with my app in rails . i am creating a simple blog in rails. but images not loaded on browser . i have put images on app/assets/images.
If you put file on assets folder, you should using pipeline, like:
<%= image_tag 'profile.png' %>
Or if all you want is the path to image without tag:
<%= asset_path 'profile.png' %>
Moreover, for using path without pipeline like host/img/profile.png, you should put images on:
/public/img/profile.png

Rails image asset tag helper

In rails you can just a regular html img tag in your templates, what is the purpose of using an asset tag helper?
<%= image_tag "header.png" %>
vs.
<img src="header.png">
I think the image_tag also uses the asset path helpers. So it will automatically use '/assets/header.png'. Also if you have asset digest turned on I think it will automatically use '/assets/header-asdfasd.png' on production.
Actually, any image_tag would be transformed into HTML img tag.
I think Rails just provides some way to create HTML in Ruby/Rails way in case you are not a HTML guy, of course you can just use HTML img tag if you like.
Not only Rails, some other render utils, like Razor in C# , provide the similar feature.

Rails angular path to image

I'm trying to reference an image in my assets/images folder from a template and having trouble. I feel like I'm missing something very basic but don't think the asset pipeline should be coming into play here. I'm using the angular templates gem. How can I properly reference this image?
* app
* assets
* images
prof_pic.jpg
* javascripts
* templates
myHtml.html
HTML
# I've tried...
<img src="../images/prof_pic.jpg">
<img src="/images/prof_pic.jpg">
<img src="prof_pic.jpg">
If it's to do with Angular you could try using something like <img ng-src="{{location}}/image-name" /> with $location.path() and then the path to the assets directory.

Best way to display images from Rails Asset Pipeline using AngularJS?

I'm working on a Rails app with an Angular-driven frontend. I want to include images from my Rails asset folders into a view that is shown only shown in certain contexts using an ng-switch directive. I tried a couple of solutions, like typing the file path of all other (Rails-served) images into the src of the <img> tag. I also tried adding an erb suffix to the coffeescript file holding the angular controller and then doing this:
$scope.logoSrc = '<%= asset_path("images/logo_grey.png") %>'
and using an ng-src directive in the image tag, but that yielded a 404.
Happy to post more of my code if it's helpful. Thanks in advance for any help!
Try the following:
$scope.logoSrc = '<%= asset_path("logo_grey.png") %>'
According to the section 2.3.3 of the Rails Guides:
If you add an erb extension to a JavaScript asset, making it something such as application.js.erb, then you can use the asset_path helper in your JavaScript code:
$('#logo').attr({
src: "<%= asset_path('logo.png') %>"
});

Why isn't my image tag working?

What is the different with
<images src="http://localhost:3000/images/logo_general.png">
and
<%= image_tag("logo_general.png") %>
Why am I having problems loading images using the first way?
Probably because the correct tag to use is
<img>
And not
<images>
There are several differences:
The image_tag generates the HTML <img> tag, not <images>
The source path is based on your asset host and asset path, so images don't break if they change. The default is relative to root, e.g. /images/
image_tag gives you an alt attribute for proper accessibility.
In development mode, it adds a random number to the image as a convenience to prevent the browser from using a cached image in case you change it.
image_tag properly closes the tag. with />.
You can try it out in the Rails Console.
image_tag("logo_general.png")
=> <img alt="Logo_general" src="/images/logo_general.png?1230601161" />

Resources