Last active
May 27, 2020 17:50
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "middleware/paperclip_middleware" | |
Rails.application.configure do | |
# other stuff | |
config.middleware.use(PaperclipMiddleware) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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