简体   繁体   中英

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

I want to store my images using the normal file storage adapter.

This is my 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"
  }

This is my model :

class User < ActiveRecord::Base

  attr_accessor :PAPERCLIP_STORAGE_OPTS

  has_attached_file :user_photo, PAPERCLIP_STORAGE_OPTS_THUMB

When a user uploads a photo - it actually does store the image in the correct location on my system:

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

However when I go to show the image, like this :

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

The image is not found, and in my logs I see the image request at this URL:

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

And the full URL created is :

https://www.myapp.com/system/users/user_photos/000/000/050/thumb/picture?1460285803 - which doesnt resolve.

How can I configure paperclip to allow my images to be stored in this particular url /opt/www/myapp/images/ and still be accessed and linked to correctly through Paperclip in my rails app?

You will have to set URL option:

for me it was:

  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'

Not sure for your case as you store images in the app folder directly so you may try(test it from console and modify it):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM