I am a newbie to Rails. In html if we want make an image visible we write it as
<img src="xyz.jpg"/>.In this way I want to know how to make that kind of stuff in Rails.How to make an image src in Rails to make it visible.
With the image_tag method.
In your case it would be:
image_tag("xyz.jpg")
This will output as:
<img src="/images/xyz.jpg" />
Which assumes that the path is images/xyz.jpg
See here for more details.
Do you mean an image tag?
image_tag("icon.png")
http://apidock.com/rails/ActionView/Helpers/AssetTagHelper/image_tag
In your ERB it will look like this:
<%= image_tag 'xyz.jpg' %>
Related
I am a newbie and trying to make a very simple rails website. I am trying to upload a photo to this website, and here is my code
<div class="col m4 center">
<%= image_tag "/assets/images/courses/4.jpg", class: "img-responsive"%>
</div>
The photo's folder is ofcourse named courses and in /assets/images/
I thought that this is ok, but my problem is, when I check my website, the photo could not loading.
It liked this
enter image description here
I am very confusing because I tried to change many photos, type, and folder, but nothing changed.
Could you please give me some ideas?
Thank you very much.
When you put an image inside /assets/images, Rails will use Sprockets to compile it and it modifies it's name with a digest hash. Instead of using the complete path, just use the part inside /images, that way rails will know what to do:
image_tag 'courses/4.jpg', class: 'img-responsive'
https://apidock.com/rails/ActionView/Helpers/AssetTagHelper/image_tag check the examples
i'm new on Rails. I can use images with image_tag or image_path.
But , i have an html template and these methods does not work in data-attributes. In my html , there is a data-background attribute which changes background of the div.
My html :
<section id="sub-header" class="section bg-parallax pt-0 pb-0" data-background="<%= image_path('bg/about.jpg') %>">
This directory is under the app/assets/images/bg. But i can't reach the image.
Where should i put these images ? and how can i reach images in this example
Try this workaround :
data-background=<%=Rails.root.to_s + "path_to_your_image" %>
I know it' ugly :/
I tried all combinations. But solution was simple. You don't need to use rails helper functions for that. I moved my images from assets to public folder and :
<section id="sub-header" class="section bg-parallax pt-0 pb-0" style='background:url("/bg/about.jpg")'>
I'm trying to add images to my post which I thought that would be easier but god I've been struggling for a while now.
I tried to use Redcarpet and added some ruby Image_tag code but didn't work. Also I tried with tag and the raw method but no luck. I don't know I've been going around but I couldn't fine anything and I think I'm not asking nothing weird or complicated ahah.
Just add pictures from my /assets/images folder if is possible.
<%= img_tag ("my_photo.jpg")%>
How do you recommend to do it?
Many thanks
I use the image_tag helper and this has always worked fine for me. You could try this:
<%= image_tag("my_photo.jpg")%>
I figured it out. I used HTML tag instead of Ruby code.
<%= raw (#post.content) %>
As I am using assets pipeline:
<img src="/assets/my_photo.jpg" alt="twd" class="img-rounded"></img>
My issue was that I was calling my photo as /images/my_photo.jpg instead of /assets/my_photo.jpg... simple as that
How can I call an image of some url external website using image_tag in a rails html view? Please, I'm looking for an alternative using image_tag not the regular html or css
EDIT Turns out OP wanted to do this b/c Heroku wasn't pulling in images from the assets folder. It is an issue that is easily resolved by reading/following the instructions here: Rails 4 images not loading on heroku .
Leaving the original answer here as it more correctly answers the original question.
image_tag works with full urls. So you could just give it the URL to the site you want to pull from. i.e:
<%= image_tag 'https://placekitten.com/800/400' %>
I'm sure you are already aware of this, but in 99.999% of situations, hot-linking images from other sites is a very bad idea.
Say you have an SVG file that is the image and it points to your Github or Linkedin profile, you could do something like this:
Setup:
In app/images/, you have some image file. For example, linkedin.svg.
Procedure
In your html.erb file, add:
<%= link_to image_tag("linkedin.svg"), "http://www.linkedin.com/in/myprofile", :target => "_blank"%>
Wait!!!!! READ CAREFULLY
Pay close attention to the space between image_tag and the parentheses(the method and the argument).
Some examples that you'll find online show a space between the image_tag and the argument. Keep them together.
Check the commas.
Last, just to be safe, make sure your link includes "http://www....."
I want to do something like:
<img src="abc.jpg"/>
But when I see Html.ActionLink I can find any overload that allows me to do that. How can I achieve the above using Action.HtmlLink?
The easiest way is not to use Html.Action link and write plain old HTML.
<a href="<%=Url.Action("Action", "Controller")%>">
<img src="<%=Url.Content("~/Images/abc.jpg")%>" />
</a>
To achive the result your asking with ActionLink you've to write your own extension.
Look at this article