the image does not appear in web browser using rails 5 - ruby-on-rails

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

Related

image_tag doesn't display external images

I have a rails 4 application and I'm using image_tags throughout the application,
I'm trying to show images on twitter. If I view the source the img src tag seems fine and I can view the image using the url that's in the img src tag.
Images in my assets folder display fine.
Any ideas what's causing this?
Thanks
It's simple, Use
<%= image_tag "https://pbs.twimg.com/media/DYKjGQLW4AAh9fI.jpg" %>
Is this something you were asking?

Rendering images from root with image_tag

I have a folder in my root 'root/tmp/database/*.JPG' that I want to show in my view with a image_tag.
My current code for this is:
<% #images.each do |image| %>
<%= image_tag image.file, class: "img-responsive" %>
<% end %>
But because image_tag uses the asset pipeline it renders the images as:
'/images/tmp/database/xotter-1.jpg'
So my question is, is it possible that I use an image_tag and get the images rendered as: '/tmp/database/*.JPG'
All assets (images, css, js etc) served in your website need to come from somewhere inside the public folder. However, the public folder can contain symbolic links, aka shortcuts, to other folders.
So, for example, if you want to serve files from #{Rails.root}/tmp/database you can make a symbolic link like so:
#from your application folder
cd public
ln -s ../tmp/database database
You should now have the appearance of having the database folder inside your public folder, and can link to a file in it with the url
"/database/xotter-1.jpg"
which corresponds to a file at
#{Rails.root}/public/database/xotter-1.jpg
These instructions assume you are in a bash-style command line shell, eg in linux or mac os. If you are in windows you may need to set the shortcut up differently.
EDIT: an answer to your follow up question about displaying all images in a folder.
Let's say you have a folder #{Rails.root}/public/database and you want to find all jpg files in it: you can do that in a variety of ways. I like Dir[], used with File.join (which is a safe way to generate file paths which accounts for extra/missing slashes, which are otherwise easy to get wrong by mistake) eg
jpg_files = Dir[File.join("public/database", "*.jpg")]
=> ["public/database/foo.jpg", "public/database/bar.jpg"]
Note that these paths are all relative to where the command is run from, which in the case of a rails server or console is the rails application folder.
If you want to link to these, you will need the path relative to the public folder, which you can get by saying
filename.gsub("public","")
=> "/database/foo.jpg"
So, to tie this together, in your template:
<% Dir[File.join("public/database", "*.jpg")).each do |file| %>
<%= image_tag file.gsub("public",""), class: "img-responsive" %>
<% end %>
Just to add to Max Williams' epic answer, if you populate your public folder with your images (instead of tmp), you'll be able to use the built-in asset_path helpers:
Computes the path to asset in public directory
asset_path "database/xotter-1.jpg" #-> /public/assets/database/xotter-1.jpg"
I appreciate this means you have to use the assets subdirectory, but there is a good reason for this...
--
Precompilation
Personally, I would put the images into the assets/images folder of your app.
The problem you'll have is that all of your images will remain "naked" in your public folder, which will both leave them open to hotlinking, but also prevent their proper use in the asset pipeline.
If you include the images in assets/images/...., you get to precompile the images when you push to production:
rake assets:precompile RAILS_ENV=production
Precompilation puts all the assets into the /public folder anyway... but more importantly, fingerprints them:
public/assets/images/database/xotter-1-[[fingerprint]].jpg
These images will still be available if you call image_tag databsae/xotter-1.jpg, but this time they'll be in their correct place.
All paths in Rails are taken from the public dir, so you can reference a link to the public dir directly, by using a relative path:
image_tag 'database/xotter-1.jpg
The difference is that if you put your images into assets/images, they are included in the asset pipeline, and thus will be accessible regardless of the environment you're running your app in.

how to insert image in rails

Using Rails, I am creating my first web-site. And I have a problem like this: I can't insert an image in my page (index.html.erb).
I put an image named "main.png" in directory "app/assets/images", and wrote that:
<img src="main.png">
But my image isn't displayed correctly. What I'm doing not right?
You should use the provided helpers by Rails to "automagically" detect paths, fingerprints, ...:
<%= image_tag "main.png" %>
Anyway, I recommend you to read the asset pipeline guides to understand how the assets works in Rails: http://guides.rubyonrails.org/asset_pipeline.html
You should use Rails helpers as markets pointed out.
By the way, the name of the image file should be the same you use as the parameter of image_tag method. You are referencing your image as main.png when the image file is main.jpg
<%= image_tag "main.jpg" %>

Ruby on Rails displaying image with image_tag src

I have an image in my app/assets/images folder and I'm in my view page trying to display an image.
<%= image_tag "NoAvatar.png" %>
When I load my page in localhost my image only shows /assets/ in its src path
<img alt="Assets" src="/assets/">
I'm not understanding why this is the issue? Am I doing something wrong? I thought this was how its supposed to link to an image in your pipeline?
Thanks!
rails 3.1 n later have a new feature called 'asset pipeline', u can read it at http://guides.rubyonrails.org/asset_pipeline.html
u can access the image in the view as
= image_tag "/assets/large/icon.png"
the image 'icon.png' resides in 'large' under 'images' in 'assets'
to see how to refer the same image in css, see Rails 3.1 and Image Assets
If your image is in assets, use:
<%= image_tag("entretien.jpg", width: 200) %>

Create image_tag for lower level foldername + stored image URL

How do I create an image_tag for a images stored in a lower level of the default assets/images directory? Currently I am storing all of my pictures in the assets/images/pictures folder.
Heres an example. Currently using #mypic instance variable with the stored base url:
pic1.jpg
.erb file:
<%= image_tag #mypic.photoUrl %>
creates an image tag
<img src="/assets/pic1.jpg" alt="pic1">
What I want is for the src to be /assets/images/pictures/pic1.jpg
OR is ruby on rails convention to put all my heterogeneous images (eg logo, profile pics, arrow pics, landscape pics, button pics) in the image directory?
Thanks!
The quick, easy solution would be to just write `<%= image_tag "pictures/#{#mypic.photoUrl}" %>.
Documentation for image_tag here.

Resources