我使用Rails Api 5.1.6作为后端。作为客户端,我使用Angular4。所有数据都存储在Oracle数据库中。数据库中的图像以“斑点”格式存储。我还使用回形针gem在Rails Api上处理图像。
因此,当我尝试在客户端上显示图像时,会出现错误“ 404 not found”。这是控制台中的错误:
获取http://localhost:4200/photos/original/missing.png 404(未找到)
我还尝试了以下链接:
但是会发生这样的错误(如果需要,我可以在此处添加完整的错误):
“#ActionController :: RoutingError:没有路由与[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
答案 0 :(得分:0)
src="http://localhost:8080/images/missing.png"
将其移动到public/images
文件夹