How to Display Image Pointed by Url in Rails

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 traditionnal image_tag

Example with Nginx :

    # 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 :

    class ImagesController < ApplicationController
    def show
    send_file File.join('/var/www/my/specific/folder',params[:name]), :disposition => 'inline'
    end
    end

    In config/routes.rb

    match '/images/:name' => 'images#show', :as => :custom_image

    Then when you access this action (via the route you defined in config/routes.rb), you have the image. So in your view you do a traditionnal image_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



Leave a reply



Submit