Ruby on rails send image with send_file and request the file in javascript

1.1k Views Asked by At

I have to retrieve an image from an existing ruby on rails backend with Javascript and show it in html.

The file is send in Image_controller with the following line:

send_file(image.path(style), type: image.content_type, disposition: 'inline')

How can I retrieve this file in javascript over the Internet an show it in my html page?

When I create a jquery request and send it to the ruby backend, I get a response in the succes function, but I don't know the format of the response data and how to show it in my html page.

1

There are 1 best solutions below

2
On

All you really need to do is respond with the image url instead of sending the file. Let javascript load the image:

 render json: { image_path: image.path(style) }

// javascript

 $.get('/myimagepath.json').done(function (res) {
   $('#target').html('<img src="' + res.image_path + '" />');
 });