Add image to layout in ruby on rails - ruby-on-rails

I would like to add an image in my template for my ruby on rails project where i currenly have the code <img src="../../../public/images/rss.jpg" alt="rss feed" /> in a the layout stores.html.erb file however this doesn't seem to load as it looks like its missing a route which i'm not sure what its supposed to be.
Any ideas please?

Anything in the public folder is accessible at the root path (/) so change your img tag to read:
<img src="/images/rss.jpg" alt="rss feed" />
If you wanted to use a rails tag, use this:
<%= image_tag("rss.jpg", :alt => "rss feed") %>

In a Ruby on Rails project by default the root of the HTML source for the server is the public directory. So your link would be:
<img src="images/rss.jpg" alt="rss feed" />
But it is best practice in a Rails project to use the built in helper:
<%= image_tag("rss.jpg", :alt => "rss feed") %>
That will create the correct image link plus if you ever add assert servers, etc it will work with those.

When using the new ruby, the image folder will go to asset folder on folder app
after placing your images in image folder, use
<%=image_tag("example_image.png", alt: "Example Image")%>

simple just use the img tag helper. Rails knows to look in the images folder in the asset pipeline, you can use it like this
<%= image_tag "image.jpg" %>

It's working for me:
<%= image_tag( root_url + "images/rss.jpg", size: "50x50", :alt => "rss feed") -%>

image_tag is the best way to do the job friend

Related

How to display image in rails with ActiveStorage

Rails: 6.1
Following the basic steps in the documentation of ActiveStorage I have added a avatar field for my User model and upload seems to be successful. But when I want to display #user.avatar I get a url for the image but the url seems to be 404. The image is not displayed.
Here is my image tag:
<img src="<%= image_path(url_for(#user.avatar)) %>" alt="Avatar for <%= #user.username %>">
<%= image_tag #user.avatar, class: 'user-image', alt: "Avatar for #{#user.username}" %>
Both of these are failing. Rendered HTML is here:
<img src="/rails/active_storage/blobs/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBCZz09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--569f9d466f5dd0d4917db040f0f87d948c0af667/paddle.png?locale=en" alt="Avatar for Someone">
Looking at the path, paddle.png is the image I uploaded and I assume upload is okay. But why doesn't it display?
Try this:
<%= image_tag #user.avatar.to_s, class: "user-image" %>
Okay, finally I found what was wrong. First thing ActiveStorage is not compatible with UUID models. What's more for a given 12hefhs-342jfsoeif-senfs-senfks UUID it takes the first consecutive digits and uses it as ID. Which is in my opinion a big issue. We don't get any error message like we do for other rails models when we try to save UUID in bigint (default id) field. To overcome this problem I had to add these migrations in this gist
Then the other problem was that I have a regex to redirect all requests to locale version of that same path. Like this:
get "/*path",
to: redirect("/#{I18n.default_locale}/%{path}", status: 302),
constraints: { path: /(?!(#{I18n.available_locales.join("|")})\/).*/ },
format: false
and this was redirecting image urls as well...
Removing this solves the problem. But introduces another one... How to redirect...

Rails image_tag full path

I have images in my assets folder and path to it
"app/assets/images/sets/img_2168.jpg"
so i want to use it like
Dir.glob("app/assets/images/sets/*.jpg") it return me "app/assets/images/sets/img_2168.jpg"
image_tag("app/assets/images/sets/img_2168.jpg") but it gives me empty image
Is it possible if so how to fix it?
Try the followings:
<%= image_tag("sets/" + File.basename(Dir.glob("app/assets/images/sets/*").first))%>
If you just provide "sets/img_2168.jpg" to image_tag, it should work.
You don't need to provide the complete path.
Try this one :
image_tag(File.read("#{Rails.root.to_s + '/assets/images/sets/img_2168.jpg'}").url)
OR
<img src="/assets/images/sets/img_2168.jpg"></img>
Ideally you should pass image object from the controller and in image_tag just get its url like :
image_tag(#image_obj.url)
The easiest way that I have found is to do this: The asset_url tag will put the full url of the asset (in this case an image called: "app-store.png") and then it will be passed to the image_tag normally.
<%= image_tag "#{ asset_url "app-store.png"}" %>
Rails 5 :
<%= image_tag(image_url('sets/img_2168.jpg')) %>
It work for me.

Proper rails image_tag path

I am trying to put an image on a mailer view using <%= image_tag("logo.png", :alt => "logo") %>. This works using an online link so I think the problem is figuring out the path. When the email is sent, the image does not appear and only the text 'logo' is showing.
I have the logo image at project_name > app > assets > images > logo.png.
I also have a public directory structured project_name > public > assets > image_and_js_files
What is the proper path I should use?
you can use
<%= image_tag("/assets/logo.png", :alt => "logo")
You have to specify the absolute link in your email in order to get it works. image_tag will only generate relative path, ex: /assets/logo.png.
Ex: https://stackoverflow.com/a/24561439/461640

Enforce rails to close image_tag

I'm rendering an image_tag in a html.erb page, but the <img> tag is not closed. I'm getting
<img alt="my_src" src="my_src"> instead of <img alt="my_src" src="my_src" />
The code looks like <%= image_tag( 'my_src.png') %>
Is there a way to enforce rails to close it?
Rails (both 3 and 4) closes the img tag by default. You must be doing something wrong. Anyway, you can use below method to generate img tag if image_tag was not working properly.
tag("img", {:src => "my_src.png"}, false)

How to write a link to a public file

In my Rails app, I have some files in the /public directory which I want to write hyperlinks to in the views. How do I do this other than writing old HTML like <a href...
Is there a neat link_to way?
There's nothing wrong with using the anchor element, after all that's what link_to outputs anyway. However, you should just be able to do:
<%= link_to 'Some File', '/some_file.zip' %>

Resources