2

I was trying to use the Addressable gem in a specific action in Rails.

My usual practice is to include the gem in the Gemfile, and then require the module where needed.

Gemfile:

gem 'addressable'

some_controller.rb:

class SomeController < ApplicationController

  def new
    require "addressable/uri"
    current_url = Addressable::URI.parse(request.original_url)
    ....
  end

end

However, I was getting a 500 error on other actions/controllers that did not use the gem.

Error during failsafe response: uninitialized constant Addressable

Finally, I removed all the code calling addressable, but kept the entry in the gemfile, and the 500 error persists on all actions. Why would this be?

Yarin
  • 173,523
  • 149
  • 402
  • 512

1 Answers1

2

Not sure why you're getting that specific error, but with a gem like Addressable where you don't want an automatic require 'addressable' performed then in your Gemfile you should have:

gem 'addressable', :require => false
smathy
  • 26,283
  • 5
  • 48
  • 68
  • @smathy- no luck! `:require => false` doesn't change anything, still getting the error on everything. And when I tried `:require => true` I get Bundler error: "cannot load such file -- addressable (LoadError)" – Yarin Jan 05 '15 at 20:44
  • And if you remove the line from your Gemfile then the error goes away? – smathy Jan 05 '15 at 20:49
  • NO! I just removed the gem line from the Gemfile, ran `bundle install`, and restarted the server- *STILL* getting errors- wtf!? – Yarin Jan 05 '15 at 21:06
  • Right, so either you have another "Addressable" somewhere in your code, or your app hasn't restarted properly. – smathy Jan 05 '15 at 21:16
  • The only place addressable shows up is as a dependency for the lanuchy gem in my Gemfile.lock, but that's been there for months. Absolutely no idea what's up- giving up on this, I'll just require the module in my initializer, which solves this crap. Thanks for the help.. – Yarin Jan 05 '15 at 22:40