1

I need a quick tip on something which seems really simple. I have some pictures inside private folder and would like to display them inside my View.

The only solution I found was this:

def show
    send_file 'some/image/url', :disposition => 'inline', :type => 'image/jpg', :x_sendfile => true
end

I've read that :disposition => 'inline' should not trigger image download and allow me to display it inside my View. The problem is that each time I trigger show action, image download is automatically activated and it is automatically downloaded. View for show action is not displayed.

How can I display that image inside my View? Thank you.

user3339562
  • 1,325
  • 4
  • 18
  • 34

3 Answers3

2

The way I do it, and I'm not saying it's perfectly by the book, is I make a root for images and an action in the controller to render it.

So, for instance, in routes.rb

match '/images/:image', to: "your_controller#showpic", via: "get", as: :renderpic

In your controller:

def showpic
    send_file "some/path/#{params[:image]}.jpg", :disposition => 'inline', 
              :type => 'image/jpg', :x_sendfile => true # .jpg will pass as format
end

def show
end

And in your view

<img src="<%= renderpic_path(your image) %>">

Here is a working example, with fewer parameters on "send_file"

def showpic
    photopath = "images/users/#{params[:image]}.jpg"
    send_file "#{photopath}", :disposition => 'inline'
end
Ruby Racer
  • 5,690
  • 1
  • 26
  • 43
  • I have a problem with this approach that `send_file "some/path/#{params[:image]}.jpg", :disposition => 'inline', :type => 'image/jpg', :x_sendfile => true` doesn't go through. Ma image source is always set on `/images/:image` and not on my path I specified for `send_file`. So it is set on url of `show.html.erb` View. – user3339562 Feb 27 '14 at 01:44
  • I will add a working example for you to work on. See my edit. – Ruby Racer Feb 27 '14 at 05:34
  • You should call renderpic_path("filename.jpg") or "/images/filename.jpg" for this to work. – Ruby Racer Feb 27 '14 at 05:40
1

I think the problem is type. From documentation:

:type - specifies an HTTP content type

So the proper HTTP content type should be image/jpeg instead of image/jpg, as you can see here. Try with:

:type => 'image/jpeg'

You also can list all available types coding Mime::EXTENSION_LOOKUP into a rails console.

Example:

Controller

class ImagesController < ApplicationController
  def show_image
    image_path = File.join(Rails.root, params[:path]) # or similar
    send_file image_path, disposition: 'inline', type: 'image/jpeg', x_sendfile: true
  end
end

Routes

get '/image/:path', to: 'images#show_image', as: :image

Views

image_tag image_path('path_to_image')
markets
  • 6,709
  • 1
  • 38
  • 48
  • Thank you. You are correct. Type was one of the problems. The problem with this approach is that if I put this `image_tag(path_to_show_action)`, image is now displayed inside `show` View. It is displayed in a new empty View. So, only image is displayed and I need that image to be displayed in my `show.html.erb` View with some other data. – user3339562 Feb 27 '14 at 01:05
  • Add a different action in your controller (route as well) with the `send_file` line and then you can use `image_tag(path_to_this_new_action)`. So you'll be able to add the image in other views. – markets Feb 27 '14 at 01:15
  • I've added different action but for some reason `send_file` doesn't go through. When I check page source as image source there is always `/images/id_number` and that is the url of `show.html.erb` for Image model. – user3339562 Feb 27 '14 at 01:34
0

You would need to have the view use an image_tag to display on the view.

Similar question was raised here: Showing images with carrierwave in rails 3.1 in a private store folder

Community
  • 1
  • 1
jamesy829
  • 428
  • 4
  • 7