`require': cannot load such file -- cancan (LoadError) - ruby-on-rails

I'm using the CanCan gem with ActiveAdmin. It is working as expected in dev, but when pushed to a staging server I get the following error:
`require': cannot load such file -- cancan (LoadError)
In my case, this is caused by the 'require' line in ActiveAdmin's CanCan adapter.
I've searched Google for potential explanations but have come up empty-handed.
What might be the cause of such an error, and how can I go about fixing it?
Update:
Here is my config/initializers/active_admin.rb:
ActiveAdmin.setup do |config|
config.authentication_method = :authenticate_user!
config.authorization_adapter = ActiveAdmin::CanCanAdapter
config.cancan_ability_class = "Ability"
config.current_user_method = :current_user
config.logout_link_path = :destroy_user_session_path
config.allow_comments = false
config.batch_actions = true
end

Restarting the entire machine fixed this.
I had tried restarting both nginx and unicorn, but I had yet to restart the machine itself. For whatever reason, this did the trick.
Thanks for your thoughts / suggestions.

Related

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

delayed_job gives "NameError: uninitialized constant"

I'm trying to move a current working task (in production and in the console) to use delayed_job in a Rails 2 app but keep getting the error:
ThermalImageJob failed with NameError: uninitialized constant Barby::Code128B
I've pored through others' code searching for an answer to no avail. Here's my code:
/lib/thermal_image_job.rb
class ThermalImageJob < Struct.new(:order_id)
def perform
order = Order.find(order_id)
order.tickets.each do |ticket|
ticket.barcodes.each do |barcode|
barcode.generate_thermal_image
end
end
end
end
/app/controllers/orders_controller.rb
Delayed::Job.enqueue(ThermalImageJob.new(#order.id))
/app/models/barcode.rb
def generate_thermal_image(format=:gif)
filename = "#{barcode}_thermal.#{format}"
temp_file_path = File.join("#{RAILS_ROOT}", 'tmp', filename)
unless FileTest.exists?(temp_file_path)
barcode_file = File.new(temp_file_path, 'w')
code = Barby::Code128B.new(barcode)
....
end
Gemfile
gem "delayed_job", "2.0.7"
gem "daemons", "1.0.10"
Well, after much head banging, I figured it out, so I'm posting this to help the next person. The problem was that it couldn't find the barby libs, so I added a require at the beginning of my class:
require "barby/outputter/rmagick_outputter"
require "barby/barcode/code_128"

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
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"

Install omniauth rails 2.3.4

I have a rails 2.3.4 app that I'd like to extend with omniauth (0.1.5). When I install omniauth gem using rvm and place require 'omniauth' in the config.rb file I get the following error:
`gem_original_require': no such file to load -- omniauth (MissingSourceFile)
The tutorials suggest using putting it in the gemfile but I am using rails 2.
When I 'gem list' omniauth is available however.
This has taken a couple of (hair-pulling) days and I am not sure how to proceed.
Am I placing the require in the correct place or is there somewhere else I could put it (aside form the obvious :-))?
Any ideas would be great....
EDIT 1: I tried config.gem "omniauth" in your environments.rb file and got
/home/mcaulejj/explorer/config/environment.rb:10: undefined local variable or method `config' for main:Object (NameError)
EDIT 2: Using RVM I updated all gems but I am still getting the same error.....
I'm exasperated at this point.
Cheers Slothihtype
Try config.gem "omniauth" in your environments.rb file.
EDIT
As per comment,
try:
require File.join(File.dirname(__FILE__), 'boot')
#insert the following here, in your config/environment.rb
if Gem::VERSION >= "1.3.6"
module Rails
class GemDependency
def requirement
r = super
(r == Gem::Requirement.default) ? nil : r
end
end
end
end
Add require 'oa-oauth' in your environment.rb file

Resources