undefined method `escape' for URI:Module in ruby 3

2.6k views Asked by At

After updating my Rails application from Rails 6.0.1 to Rails 7.0.2.3

I am getting issue with the gem "paperclip", '~> 6.1.0'

while using it in application is gives error:

ActionView::Template::Error (undefined method `escape' for URI:Module
Did you mean?  escape_once):

Usage in my application:

<%= image_tag current_user.image.url('med'), width: "36px" %>

How to resolve this issue when bug is present in the ruby gemfile itself, thanks in advance.

2

There are 2 answers

1
vidur punj On BEST ANSWER

The solution to this situation do a monkey patching to the missing method in library.

add a ruby filke uri_escape.rb inside the initializers folder:

add lines for monkey patching:

module URI
  def self.escape(url)
    encode_www_form_component(url)
  end
end

and its done.

0
Dave Collins On

If you are having this problem due to the now-deprecated "paperclip" gem, and you don't want to switch to ActiveStorage yet, there is a solution: The kt-paperclip gem (https://rubygems.org/gems/kt-paperclip) is a fork of Paperclip.

They have fixed this URI issue and switching to kt-paperclip will solve the above paperclip error and allow you to continue without migrating to ActiveStorage.