簡體   English   中英

Rails和Paperclip在特定路徑中存儲圖像會設置錯誤的URL

[英]Rails and Paperclip storing images in a specific path sets wrong URL

我想使用普通的文件存儲適配器存儲圖像。

這是我的PAPERCLIP_STORAGE_OPTS:

PAPERCLIP_STORAGE_OPTS = {
    :styles => { :thumb => '170x170!#', :medium => '450x300!>', :large => '600x400!>',:desktop => '750x300!>'},
    :convert_options => { :all => '-quality 100' },
    :processor       => [ :papercrop ],
    :path => "/opt/www/myapp/images/:class/:attachment/:id_partition/:style/:filename"
  }

這是我的模型:

class User < ActiveRecord::Base

  attr_accessor :PAPERCLIP_STORAGE_OPTS

  has_attached_file :user_photo, PAPERCLIP_STORAGE_OPTS_THUMB

用戶上傳照片時-實際上確實將圖像存儲在系統上的正確位置:

/opt/www/myapp/images/users/user_photos/000/000/050/original/picture

但是,當我去顯示圖像時,像這樣:

<%=image_tag current_user.user_photo.url(:thumb), :height=> "30", :width=> "30" %>

找不到圖片,在我的日志中,我在以下URL上看到圖片請求:

ActionController::RoutingError (No route matches [GET] "/system/users/user_photos/000/000/050/thumb/picture"):

創建的完整URL是:

https://www.myapp.com/system/users/user_photos/000/000/050/thumb/picture?1460285803-無法解決。

如何配置回形針,以允許將圖像存儲在此特定的url / opt / www / myapp / images /中,並且仍然可以通過Rails應用程序中的回形針對其進行訪問和正確鏈接?

您將必須設置URL選項:

對我來說是:

  has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>", :small=>"60x60>" },
                         :path => ':rails_root/public/system/:class/:id/:style/:filename',
                         :url => '/system/:class/:id/:style/:filename'

由於您直接將圖像存儲在app文件夾中不確定情況,因此可以嘗試(從控制台對其進行測試並進行修改):

:path => "/opt/www/myapp/images/:class/:attachment/:id_partition/:style/:filename",
:url => '/images/:class/:attachment/:id_partition/:style/:filename'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM