Heroku uninitialized constant Rails:Initializer (NameError) (noWWW Rack Middleware) - ruby-on-rails

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

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.

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.

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

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"

Related

Sprockets::Rails::Helper::AssetNotPrecompiled in ResqueWeb

I've been trying to fix this for about two days but I'm getting no where. I'm trying to get the resque-web page to show but I keep running into the error
Sprockets::Rails::Helper::AssetNotPrecompiled in ResqueWeb::Overview#show
Message shown
ActionView::Template::Error (resque_web/plugins/resque_scheduler/application.css):
12: file_path = "#{p.name.underscore.downcase}/application.css"
13: if (Rails.application.assets && Rails.application.assets.find_asset(file_path)) ||
14: (Rails.application.assets_manifest && Rails.application.assets_manifest.assets[file_path])
15: stylesheet_link_tag "#{p.name.underscore.downcase}/application"
16: end
17: end.join("\n").html_safe
18: %>
what is being called from the stack
def raise_unless_precompiled_asset(path)
raise Helper::AssetNotPrecompiled.new(path) if #check_precompiled_asset && !precompiled?(path)
end
end
end
gem versions
resque (2.0.0)
resque-multi-job-forks (0.5.1)
resque-scheduler (4.4.0)
resque-web (0.0.12)
sprockets (4.0.2, 3.7.2)
sprockets-rails (3.2.2)
routes.rb
require "resque_web"
require 'resque-scheduler'
require 'resque/scheduler/server'
Rails.application.routes.draw do
mount ResqueWeb::Engine => "/resque_web"
root to: "pages#home"
.
.
.
end
Ideally it will be 'admin/resque_web' for production purposes.
I've tried this fix by "bitterloa" https://github.com/resque/resque-web/issues/106 because my file structure is set the same using webpacker
I've looked and the development.rb to check that debugging is false
I've recompiled my assets after destroying them using sprock-rails rails assets:clobber
Any ideas where I can look or if anyone else has run into this problem lend a hand please.
Run commands from terminal shows that resque is running and accepting schedule jobs from my scheduler.yml file I just can't get the css for it to work.
If you need any more info let me know.
Thanks
resque comes with a built-in server. You do not need resque-web and it seems it is not maintained (last commit was on 2018).
Let's try this:
gem 'resque', require: 'resque/server'
# routes.rb
mount Resque::Server.new, :at => "admin/resque"
Make sure you allow just admins to access to that page in production. For that you could read about Route Constraints to do something like:
constraints IsResqueAllowed do
mount Resque::Server.new, :at => "admin/resque"
end
class IsResqueAllowed
def self.matches?(request)
# use the request to do something
end
end
More information about securing the route here.
If you want to use resque-web then in order to fix this issue you will need to add in assets.rb:
Rails.application.config.assets.precompile += %w[idle lifebuoy poll rails working].map { |img| "resque_web/#{img}.png" }
Rails.application.config.assets.precompile += %w[resque_web/application.css]
Rails.application.config.assets.precompile += %w[resque_web/application.js]
Subsequently you will need to run: rake assets:precompile
And the issue should be fixed.

How to add middleware in Rails 4.2 application

I am trying to learn Middlewares and been practising how to mount it in the Rails application. I have followed the railscast
So far I have implemented these steps:
1) Created a new Rails 4.2 application called: Blog
2) Added a file in the lib folder named as response_timer.rb.
class ResponseTimer
def initialize(app)
#app = app
end
def call(env)
[200, {"Content-Type" => "text/html"}, "Hello World"]
end
end
3) Added config.middleware.use "ResponseTimer" in application.rb.
config.middleware.use "ResponseTimer"
But as i'm hitting the command rake middleware in the terminal, it is reporting this error:
rake aborted!
NameError: uninitialized constant ResponseTimer
I tried also to add the config.middleware.use "ResponseTimer" in the development.rb but again facing the same error.
What am i missing here?
Please help.
Referenced article: http://guides.rubyonrails.org/rails_on_rack.html
Middleware has to have an accompanying module / class, and needs to be loaded in the app before it can be referenced. The way to do this in Rails is with autoloading (lib files aren't autoloaded by default):
#config/application.rb
config.autoload_paths += Dir["#{config.root}/lib/**/"]
config.middleware.use "ResponseTimer"
The above should work for you.
I followed this answer: https://stackoverflow.com/a/24122424
I tried it before but maybe missed something before.
In appliation.rb
require 'rails/all'
require_relative '../lib/response_timer'
module Blog
class Application < Rails::Application
...
config.middleware.use ResponseTimer
end
end

Correct way to load middleware for rails and rspec

So... I have a bit of a conundrum.
I've written some rack middleware, it's stored on disk at app/middleware/eat_bacon.rb, it looks something like
module Middleware
class EatBacon
def initialize(app)
#app = app
end
def call(env)
Thread.current[:mouth] = 'Bacon'
#app.call(env)
end
end
end
I'm trying to load / use some middleware in a rails-api (rails 3.2.19)
In my config/application.rb I've gotten the middleware to load in two different ways, one way works when running the app, the other way works for rspec
Works for running app
config.middleware.insert_before 0, 'Middleware::EatBacon'
but when I run rspec, I get
/Volumes/HardDrive/Me/.rvm/gems/ruby-2.0.0-p451#bacon/gems/activesupport-3.2.19/lib/active_support/inflector/methods.rb:230:in `block in constantize': uninitialized constant Middleware (NameError)
Works for RSpec
config.after_initialize do
config.middleware.insert_before 0, 'Middleware::EatBacon'
end
But now the middleware is not loaded when the application runs, ie Thread.current[:mouth] never gets bacon
Seems weird to have to implement this differently in config/environments/test.rb and config/environments/production.rb, but I guess thats what I'll end up doing unless someone has a better idea
The way I solved this was to first move the middleware to the lib folder:
lib/middleware/eats_bacon.rb
AND Then, in config/application.rb I added a require_relative, so that file now looks like
...
Bundler.require(*Rails.groups)
require_relative '../lib/middleware/eats_bacon.rb'
module BaconEaterApp
class Application < Rails::Application
...
Then at the bottom of that file
...
# Middleware
config.middleware.insert_before 0, 'Middleware::EatBacon'
...

uninitialized constant after capistrano delpoy:cold - nginx, unicorn

I have setup capistrano for deployment with the exact same config/setup as found in the railscasts Pro episode http://railscasts.com/episodes/335-deploying-to-a-vps?view=asciicast.
All of the deploy:check, status and cold tasks run and complete successfully (after some tinkering). However, the app not running and shows the classic "something went wrong" error page. When I check my unicorn.log it shows the error below:
I have tried requiring the module before including it to address threadsafe issues and also autoloading the absolute path in application.rb. Note this all works in development environment.
How can I amend my code to fix this NameError issue?
unicorn.log
E, [2013-10-16T04:15:00.313177 #12996] ERROR -- : uninitialized constant AnswersController::Teebox (NameError)
/home/andrew/rails/teebox/releases/20131016032538/app/controllers/answers_controller.rb:5:in `<class:AnswersController>'
/home/andrew/rails/teebox/releases/20131016032538/app/controllers/answers_controller.rb:1:in `<top (required)>'
answers_controller.rb
class AnswersController < ApplicationController
before_filter :authenticate_user!, except: [:index, :show]
load_and_authorize_resource
require 'teebox/commentable'
include Teebox::Commentable # Offending line
...
end
lib/teebox/commentable.rb
require 'active_support/concern'
module Teebox::Commentable
extend ActiveSupport::Concern
included do
before_filter :comments
end
def comments
#comment = Comment.new
end
end
application.rb
# Custom directories with classes and modules you want to be autoloadable.
config.autoload_paths += %W(#{config.root}/decorators)
config.autoload_paths += %W(#{config.root}/lib)
config.autoload_paths += %W(#{config.root}/lib/teebox/commentable.rb)
specs:
capistrano 2.15.5
rails 3.2.14
ruby 1.9.3-p488
ubuntu 12.04
If anyone needs more code just shout.
I had an naming convention which wasn't an issue on Mac but was an issue on a case sensitive Linux box.
I had a lib/Teebox/commentable.rb folder structure and was then calling:
include Teebox::Commentable.
So I switched this to lib/teebox/commentable.rb (Lowercase teebox) and this solved the error.

Rails precompile constant uninitialized

I wanted to preload the configuration (from ".yml" files). In one of my initializer files (config/initializers/facebook.rb) I have following line of code:
FACEBOOK_CONFIG = YAML.load_file("#{Rails.root}/config/facebook.yml")[Rails.env]
So, it works like a charm in the "DEVELOPMENT" mode. Once I switch to the production mode, it keeps telling me, that FACEBOOK_CONFIG is an uninitialized constant for my "facebook.js.coffee.erb" file, located in assets/javascript (If it matters), if I want to o "rake assets:precompile". I've tried doing random stuff, like: RAILS_ENV=production bundle exec rake assets:precompile or
rake assets:precompile:all
, but no luck
I have tried assigning "initialize_on_precompile = true" variable for my production environment (although, it should be true by default), just in case.
Why it doesn't work in production mode (But, I want to emphasise, that it does work(!) in the development environment).
Can someone help with that one ?
I encountered exactly the same problem. This is because your javascript(coffescript) file makes reference to a constant that is defined in an initializer. Because it is precompiled before the initializer the app throws an error.
This is the simple solution I found. You place this code at the bottom of your application.rb file in config:
module AssetsInitializers
class Railtie < Rails::Railtie
initializer "assets_initializers.initialize_rails",
:group => :assets do |app|
require "#{Rails.root}/config/initializers/facebook.rb"
end
end
end
It manually loads up certain files from the initializer folder. It solved my problem.
Hopefully this was the issue for you as well.
module Rails
class << self
def facebook_config
##facebook_config ||= nil
end
def facebook_config=(facebook_config)
##facebook_config = facebook_config
end
end
end
Rails.facebook_config = YAML.load_file("#{Rails.root}/config/facebook.yml")[Rails.env]
# And you can use it like this in anywhere:
puts Rails.facebook_config

Resources