U
U
uib2018-10-12 08:19:36
Ruby on Rails
uib, 2018-10-12 08:19:36

Why do I get a "404 Not Found" error when I try to display an image?

I'm using Rails Api 5.1.6 as a back end and as an Angular 4 client. All data is stored in an Oracle database. Images in Oracle are stored in the "Blob" format. To work with images on the Rails Api, I use the "paperclip" gem.
So when I try to display an image on the client, I get a "404 not found" error. Here is the error in the console:

GET localhost:4200/photos/original/missing.png 404 (Not Found)

5bc02dc633e57308312648.png
Tried following this link:
localhost:3000/photos/original/missing.png

, but such an error appeared (If necessary, I can throw off the full error):
"#ActionController::RoutingError: No route matches [GET] "/photos/original/missing.png""

JSON:
[
{
l_users_id: 65,
photo: "/photos/original/missing.png"
},
{
l_users_id: 1,
photo: "/photos/original/missing.png"
},
{
l_users_id: 1357,
photo: "/photos/original/missing.png"
},
{
l_users_id: 1358,
photo: "/photos/original/missing.png"
},
{
l_users_id: 51,
photo: "/photos/original/missing.png"
},
{
l_users_id: 2,
photo: "/photos/original/missing.png"
},
{
l_users_id: 1360,
photo: "/photos/original/missing.png"
},
{
l_users_id: 0,
photo: "/photos/original/missing.png"
},
{
l_users_id: 1372,
photo: "/photos/original/missing.png"
},
{
l_users_id: 1371,
photo: "/photos/original/missing.png"
},
{
l_users_id: 1370,
photo: "/photos/original/missing.png"
},
{
l_users_id: 1373,
photo: "/photos/original/missing.png"
}
]

photos_controller.rb:
class PhotosController < ApplicationController

    def index
        @photos = Photo.all

    array = @photos.map do |photo|
        photo_push = {
            l_users_id:      photo.l_users_id,
            photo:           photo.photo.url
        }

        photo_push
    end

    render json: array
  end

    def show
        @photo = Photo.find(params[:id])
        photo_hash = {
            l_users_id:      photo.l_users_id,
            photo:           photo.photo.url
        }

        render json: photo_hash
    end

end

photo.rb:
class Photo < ActiveRecord::Base
  self.primary_key = "l_users_id"

  has_attached_file :photo

  validates_attachment_presence :photo
  validates_attachment_content_type :photo, :content_type => ['image/jpeg', 'image/png']

end

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Vorobyov, 2018-10-12
@AlexVor

Error 404 indicates that the file does not exist, that is, look for an error in the path to the file.
Photo and PhotoS no?

K
Karim Kyatlottyavi, 2018-10-24
@constXife

By default, paperclip uses the file system to store files and hardly has built-in support for displaying files from the database, so missing everywhere instead of a URL.
1. You will need to independently make a display in some controller and give an image like this:

class PhotosController
  def render_image
    photo = Photo.find(params[:photo_id)
    send_data photo.photo, type: 'image/png', disposition: 'inline'
  end
end

This will give the image from the database.
2. You need to register a route to this controller. Something like:
# config/routes.rb

get '/photos/:photo_id' => 'photos#render_image'

At this stage, you can try to go to the URL in the browser /photos/1, the image should appear in the browser.
3. Specify paperclip to use your route to generate url to images.
Or look for a ready-made solution in the form of a gem in Google: paperclip render database gem

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question