를 표시하는 방법의 이미지 개인 폴더 내부에 볼?
-
21-12-2019 - |
문제
필요한 빠른 팁 무언가에있는 것은 정말 간단합니다.나는 몇 가지 사진 내부에 전용 폴더로 표시 하고 싶은 그들이 안됩니다.
유일한 해결책을 발견이 있었다:
def show
send_file 'some/image/url', :disposition => 'inline', :type => 'image/jpg', :x_sendfile => true
end
내가 읽는 :disposition => 'inline'
지 트리거 이미지 다운로드 수를 표시하는 안됩니다.문제는 그 때마다 트리거 show
작업 이미지 다운로드를 자동으로 활성화되고 자동으로 다운로드됩니다.보기 show
조치를 표시하지 않습니다.
어떻게 표시할 수 있습니는 이미지 내부에 내 보?감사합니다.
해결책
내가하는 방식으로, 나는 그것이 그 책이 완벽하게 말하지 않는다고 말하지 않는다, 나는 그것이 렌더링 할 수있는 이미지와 조치를 위해 루트를 만들고있다.
그래서, 예를 들어, routes.rb
match '/images/:image', to: "your_controller#showpic", via: "get", as: :renderpic
.
컨트롤러에서 :
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
.
및 귀하의 견해
<img src="<%= renderpic_path(your image) %>">
.
여기에 "send_file"
에 매개 변수가 적은 작업 예제가 있습니다.def showpic
photopath = "images/users/#{params[:image]}.jpg"
send_file "#{photopath}", :disposition => 'inline'
end
. 다른 팁
나는 생각한 문제이 type
.에서 문서:
:type - specifies an HTTP content type
그래서 적절한 HTTP 콘텐츠 유형 image/jpeg
대 image/jpg
, 수있는,당신 여기에 참조.보십시오:
:type => 'image/jpeg'
당신은 또한 목록을 사용할 수 있는 모든 종류 코딩 Mime::EXTENSION_LOOKUP
으로 레일을 엽니다.
예제:
컨트롤러
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
노선
get '/image/:path', to: 'images#show_image', as: :image
보
image_tag image_path('path_to_image')
뷰가 뷰에 표시 할 Image_TAG를 사용해야합니다.
유사한 질문이 여기에서 올랐습니다. 개인 저장소 폴더의 Rails 3.1의 CarrierWave가있는 이미지 표시
제휴하지 않습니다 StackOverflow