Skip to content

Instantly share code, notes, and snippets.

@cyu
Created December 5, 2013 18:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cyu/7810850 to your computer and use it in GitHub Desktop.
Save cyu/7810850 to your computer and use it in GitHub Desktop.
Protocol relative URLs in Paperclip
# config/initializers/paperclip.rb
require 'paperclip/protocol_relative_url_support'
Paperclip::Attachment.send :include, Paperclip::ProtocolRelativeURLSupport
# lib/paperclip/protocol_relative_url_support.rb
module Paperclip
module ProtocolRelativeURLSupport
def self.included(base)
base.alias_method_chain :url, :protocol_relative
end
def url_with_protocol_relative(style_name = default_style, options = {})
u = url_without_protocol_relative(style_name, options)
if options and options[:protocol_relative]
u.gsub(/^https?:/, '')
else
u
end
end
end
end
# get a protocol relative URL like this
product.image.attachment.url(:original, protocol_relative: true)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment