How to display image pointed by URL in Rails
You can also use the action view image_tag
helper:
<%= image_tag 'http://power.itp.ac.cn/~jmyang/funny/fun4.jpg' %>
Alternatively you can just use the regular HTML tags:ERB:
<img src="http://power.itp.ac.cn/~jmyang/funny/fun4.jpg">
Haml:%img{ src: "http://power.itp.ac.cn/~jmyang/funny/fun4.jpg" }
Slim:img src="http://power.itp.ac.cn/~jmyang/funny/fun4.jpg"
Show image with Rails server URL
Use asset helper method image_tag()
. You should surely read official guide's Coding Links to Assets.
<%= image_tag('/images/2019-12/image1.png') %>
should work for you.Rails use asset pipeline and serve files from asset load paths (default to app/assets
, lib/assets
, vendor/assets
)
In development files under those load path are served by app server.
In production files under those load path are pre-compiled into /public/assets
and served by web server such as nginx.
The image_tag
method with
- relative file path e.g. 'file.png' searches for
/public/assets/file.png
- absolute file path e.g. '/images/file.png' searches for
/public/images/file.png
Showing images with Rails from database
@product.images.where(image_type: 2).first
returns a first record from database, but for image_tag
you should specify url
or path
for returned records.
Use url
attribut or what you have:
<%= image_tag @product.images.where(image_type: 2).first.url, :alt => 'product_image' %>
^^^
How can I display an image whose path is outside my Ruby on Rails project directory using rails 3?
I see two ways :
- On top of your rails server, in production and sometimes in dev, you have to use a web server like apache or nginx. With these, you can serve files from other directories for specific URL. E.G. you can make
http://yourapp.com/images/
serving files from a specific dir. In rails, display the image with a traditionnalimage_tag
# Find the right `server` section which you currently use to serve your rails app
server {
listen 80;
# Add this
location /images {
root /var/www/my/specific/folder;
}
location / {
#...
#... Here you should already some code to proxy to your rails server
}
}
With that, when you access to `yourserver.com/images`, nginx serve your specific folder and not your rails app. Then in your app view :
<%= image_tag 'http://yourserver.com/images/my_pic.jpg' %>
If you can't access your server settings, you can serve an image file from a controller action with
send_file
In a controller :
Inclass ImagesController < ApplicationController
def show
send_file File.join('/var/www/my/specific/folder',params[:name]), :disposition => 'inline'
end
endconfig/routes.rb
Then when you access this action (via the route you defined inmatch '/images/:name' => 'images#show', :as => :custom_image
config/routes.rb
), you have the image. So in your view you do a traditionnalimage_tag
with this URL :<%= image_tag custom_image_path( 'my_pic.jpg' ) %>
OR
<%= image_tag custom_image_url( 'my_pic.jpg' ) %>
How to pass a variable from a query into image url, ruby on rails?
You can use this:
<%= image_tag("example.com/#{product.id}.jpg") if product.present? %>
how to insert image in rails
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
Related Topics
Ruby: Converting a Nested Ruby Hash to an Un-Nested One
Difference Between Render and Yield in Rails
Filtering Sensitive Data with Vcr
Rails How to Tell If a Sidekiq Worker Is Done with Perform_Async
Automating Ssh to Windows with Ruby
How to "Break" Out of a Case...While in Ruby
Good Forms Helpers for Sinatra
How to Get Nokogiri to Parse and Return an Xml Document
Foreign Key (Class_Id) Not Populating in Belongs_To Association
Why Should @@Class_Variables Be Avoided in Ruby
Ruby 1.9 How to Convert Array to String Without Brackets
How to Automatically Escape HTML Content Using Jekyll and Markdown
How to Set in a Middleware a Variable Accessible in All My Application
Will_Paginate Find Out If I'M on The Last Page
Permission Error When Trying to Install Rails (Osx)
Should I Delete Migration After Rollback