简体   繁体   中英

rails paperclip has_attached_file path with :slug?

I'm trying to save my attached files to s3 -- but instead of the :id being saved in the path, I'd like to use the :slug that's being written to the db from the friendly_id gem. But when I put :slug in the path, I get a folder named :slug rather than the actual :slug variable.

I'm curious how to best insert the :slug into where the image is being saved. Curious as well about what parameters are exposed to paperclip when has_attached_file is called.

You must use paperclip interpolations. This works for me with paperclip 3.5.2

class User < ActiveRecord::Base
  extend FriendlyId
  friendly_id :name, use: [ :slugged, :finders ]

  Paperclip.interpolates :slug do |attachment, style|
    attachment.instance.slug
  end

  has_attached_file :picture,
    styles: { large: "512x512!", medium: "256x256!", thumb: "128x128!" },
    default_url: "/images/:class/:style/missing.png",
    hash_secret: "xxxxxxxxxxxxxxxxxxxxxx",
    url: "/system/:class/:attachment/:slug/:style/:hash.:extension",
    path: ":rails_root/public/system/:class/:attachment/:slug/:style/:hash.:extension"
end

It also demonstrates the use of hashed file names to hide private information in the file name. Url and path must match unless you set up some routing in the web server to get the file name from the url.

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