1

fancybox를 사용하여 이미지를 표시하려고합니다. 애셋 폴더의 기존 이미지처럼 정적 이미지를 사용하면 모든 것이 정상적으로 작동합니다. 그러나 carrierwave를 사용하여 저장 한 업로드 된 이미지를 사용하려고하면 "요청한 콘텐츠를로드 할 수 없습니다."라는 메시지가 항상 표시됩니다.Fancybox가 CarrierWave로 업로드 된 사진을 표시하지 않습니다.

내가 그림을 표시하기 위해 사용하고 코드는 다음입니다 :

응용 프로그램/모델/upload.rb

class Upload 
    include Mongoid::Document 
    include Mongoid::Timestamps 

    mount_uploader :file, UploadUploader 

    # Fields 
    field :file, type: String 
    field :filename, type: String 
    field :file64, type: String 
    field :size_file, type: Integer 
    field :file_extension, type: String 

    # Validations 
    validates_presence_of :file 

    # Hooks 
    before_validation do 
    if not self.file.url and self.filename and self.file64 
     sio = CarrierwaveStringIO.new(Base64.decode64(self.file64)) 
     sio.original_filename = self.filename 
     self.file = sio 
     self.filename = nil 
     self.file64 = nil 
    end 
    end 

    before_save do 
    self.size_file = self.size 
    self.file_extension = File.extname(self.file.to_s)[1..-1] 
    end 

    def base64 
    MIME::Types.type_of(self.file.url).first.content_type 
    end 

    def size 
    self.file.size 
    end 
end 

응용 프로그램/전망 ...

... 
= link_to upload_path(upload.id), class: 'fancybox' do 
    = image_tag upload_path(upload.id), height: 174, width: 256 
... 

app/assets/javascript ...

... 
$(".fancybox").fancybox() 
... 

나는 코드 제작에 많은 노력을 기울였다. 예를 들어 뷰에서 link_tag의 "href"와 image_tag의 "src"를 업로드의 표시 경로로 변경하지만 여전히 작동하지 않습니다. 또한 AJAX를 사용하여 그림을로드하고 fancybox의 "content"속성을 사용하려고했지만 동일한 결과를 얻었습니다. 마지막으로 업로드 컨트롤러에 대한 show 액션을 만들었습니다. 여기서는 사진을 표시하고 fancybox의 "content"및 "type"속성을 사용하여 html을 사용했지만 사진이 보이지 않습니다 (URL과 일치하는 경로가 없음). 사진, 쇼 동작이 아님).

나는 작동하는데 많은 시간을 투자했지만 아무 것도 찾을 수 없다. 미리 감사드립니다.

편집 :

응용 프로그램/컨트롤러/upload_controller.rb

class UploadsController < ApplicationController 
    load_and_authorize_resource 

    respond_to :html, :json 

    def show 
    if request.format.html? 
     content_type = MIME::Types.type_for(@upload.file.url).first.content_type 
     send_file @upload.file.url, content_type: content_type, disposition: 'inline', :x_sendfile => true 
    else 
     respond_with @upload, api_template: :general 
    end 
    end 

    ... 

응용 프로그램/업 로더 업로드/_...

class UploadUploader < CarrierWave::Uploader::Base 
    storage :file 

    def store_dir 
    "#{ Rails.root.to_s }/uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" 
    end 

    def cache_dir 
    "#{ Rails.root.to_s }/tmp/uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" 

    def extension_white_list 
    %w(jpg jpeg png bmp tif) 
    end 
end 

응용 프로그램/뷰/업로드/show.html.haml

= image_tag @upload.file_url.to_s 
+1

은 업로드 된 URL을 사용하여 이미지를로드 중입니다. 그렇다면 URL이 공개 된 경우 jsfiddle에 코드를 추가 할 수 있습니까? – Dave

+0

방화 광과 함께 URL을 볼 때 해당 URL에 깨진 이미지가 표시되므로 이미지가로드되는 지 확실하지 않습니다. 사진이로드되었는지 어떻게 확인할 수 있습니까? 죄송하지만 잠시 jsfiddle에 코드를 추가 할 수 없습니다. – ReyLitch

+1

그 URL을 복사하여 새 브라우저 탭에 붙여 넣으십시오. 아마존에서 호스팅했다면 이미지 URL에 액세스 할 때 문제가 무엇인지 표시해야합니다. 서버의 구성 설정으로 인해 사용자가 이미지에 액세스 할 수있는 권한이없는 경우가있을 수 있습니다. 그리고 서버에 이미지를 공개 한 경우 이미지를 확실히 볼 수 있어야합니다. (이미지 URL로 질문 업데이트) – Dave

답변

1

유형 옵션을 지정하면 콘텐츠 유형이 강제 적용됩니다. 이것은 "이미지", "AJAX ''은 iframe ','SWF '또는'인라인 '

$(".fancybox11").fancybox({ 
type: 'iframe', width: 380, height: 280 
}) 

로 설정 될 수 링크 fancybox api options을 참조.

+1

그거야. 당신의 도움을 주셔서 감사합니다. 업로드 리소스의 더 많은 코드로 주제를 업로드했습니다. – ReyLitch

관련 문제