David
David

Reputation: 213

Heroku uninitialized constant Rails:Initializer (NameError) (noWWW Rack Middleware)

I'm running Cedar stack on Heroku, rails 3.1.3.

using: http://trevorturk.com/2009/11/05/no-www-rack-middleware/

When I push to Heroku, I get:

app[web.1]: => Rails 3.1.3 application starting in production on http://0.0.0.0:15548
app[web.1]: => Call with -d to detach
app[web.1]: => Ctrl-C to shutdown server
app[web.1]: Exiting
app[web.1]: /app/config/environment.rb:7:in `<top (required)>': uninitialized constant Rails::Initializer (NameError)

Anyone have any ideas whats going on?

Here's my environment.rb (AppName = proper name for my app. i.e. thats not the issue)

# Load the rails application
require File.expand_path('../application', __FILE__)

# Initialize the rails application
AppName::Application.initialize!

Rails::Initializer.run do |config|
  config.middleware.use "NoWWW" if RAILS_ENV == 'production'
end

lib/no_www.rb:

class NoWWW

  STARTS_WITH_WWW = /^www\./i

  def initialize(app)
    @app = app
  end

  def call(env)
    if env['HTTP_HOST'] =~ STARTS_WITH_WWW
      [301, { 'Location' => Rack::Request.new(env).url.sub(/www\./i, '') }, ['Redirecting...']]
    else
      @app.call(env)
    end
  end

end

Upvotes: 2

Views: 2898

Answers (4)

Baub
Baub

Reputation: 5044

I had this issue; I know it's a late answer but I wanted to add this for whoever needed it.

Make sure that your OmniauthCallbacksController IS IN A users FOLDER. "app/controllers/users/omniauth_callbacks_controller.rb"

Upvotes: 0

Dru
Dru

Reputation: 9820

I had the same problem using Rails 3.1. I ended up using this post. It appears to be more involved than other solutions but there are really only two steps.

Make sure to change yoursite.com in the code. I overlooked this and had to rush another deployment after the fix.

Upvotes: 1

Brent
Brent

Reputation: 11

It looks like your middleware file is not being loaded. Place your middleware class, no_www.rb in app/middleware. This way it will be auto-loaded by Rails. Then add your config statement to application.rb, near the end.

    ...

    # Configure Rack middleware
    config.middleware.use 'NoWWW'

  end
end

Upvotes: 0

ezkl
ezkl

Reputation: 3851

The error you're receiving is telling you that you're referencing an old version of Rails. Rails 3.1 initializes quite a bit differently than it did 2 years ago when that article was written. In particular, the problem is with the now deprecated Rails::Initializer in this block:

Rails::Initializer.run do |config|
  config.middleware.use "NoWWW" if RAILS_ENV == 'production'
end

You might have more luck with rack-rewrite. Regardless, check out the official Rails documentation for a good breakdown of current configuration and initialization.

Upvotes: 0

Related Questions