Skip to content

Instantly share code, notes, and snippets.

@flanger001
Last active May 27, 2020 17:50
Show Gist options
  • Save flanger001/c8ea416d27b0afa749b0ba84ef9c0d3c to your computer and use it in GitHub Desktop.
Save flanger001/c8ea416d27b0afa749b0ba84ef9c0d3c to your computer and use it in GitHub Desktop.
This exists because Paperclip emits a lot of 404 errors if you clone your development db from production. If you do not clone your development db from production, you probably should not use this.
require "middleware/paperclip_middleware"
Rails.application.configure do
# other stuff
config.middleware.use(PaperclipMiddleware)
end
# lib/middleware/paperclip_middleware.rb
class PaperclipMiddleware
attr_reader :app
def initialize(app)
@app = app
end
def call(env)
if env.fetch("REQUEST_METHOD") == "GET" && env.fetch("REQUEST_PATH").match?("paperclip")
log_tagged { "OVERRIDING PAPERCLIP IN DEVELOPMENT" }
# Decide what you want to do here
# I decided to serve a static image
file = File.read(Rails.root.join("public", "no-photo.png"))
[200, { "Content-Type" => "image/png" }, [file]]
else
app.call(env)
end
end
private
def log_tagged
Rails.logger.tagged("PAPERCLIP MIDDLEWARE") { Rails.logger.debug(yield) }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment