Reputation: 3422
I have created a class in my lib folder inside my rails app lib/pundit/current_context.rb :
class CurrentContext
attr_reader :user, :account_asso
def initialize(user, account_asso)
@user = user
@account_asso = account_asso
end
end
This class is then called in my base_controller :
def pundit_user
CurrentContext.new(current_user, account_asso)
end
I am always getting :
NameError - uninitialized constant Api::V1::BaseController::CurrentContext:
I thought it might be because I am not loading the files inside lib ?
so I added config.autoload_paths << Rails.root.join('lib')
inside my config/application.rb file :
require File.expand_path('../boot', __FILE__)
require "rails"
# Pick the frameworks you want:
require "active_model/railtie"
require "active_job/railtie"
require "active_record/railtie"
require "action_controller/railtie"
require "action_mailer/railtie"
require "action_view/railtie"
require "sprockets/railtie"
# require "rails/test_unit/railtie"
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)
module QuickBedApi
class Application < Rails::Application
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
# config.time_zone = 'Central Time (US & Canada)'
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
# config.i18n.default_locale = :de
config.middleware.insert_before 0, "Rack::Cors" do
allow do
origins '*'
resource '*', :headers => :any, :methods => [:get, :post, :options]
end
end
# Do not swallow errors in after_commit/after_rollback callbacks.
config.active_record.raise_in_transactional_callbacks = true
config.autoload_paths << Rails.root.join('lib')
end
end
Unfortunately I am still getting the error. How can I solve that ?
Upvotes: 3
Views: 1485
Reputation: 43
I had the same problem to call a Module of my lib folder.
Even the "config.autoload_paths += %W(#{config.root}/lib)" didn't work for me.
After some researchs I figured out that i had to put " require 'my_modules' "in addition of the " include MyModules " in my file to be able to call it.
so you can try to require it
Upvotes: 0
Reputation: 196
To include all subdirectories of lib/ directory automatically,
config.autoload_paths += Dir["#{config.root}/lib/**/"]
Upvotes: 4
Reputation: 16514
try include all the folders of lib
:
config.autoload_paths.concat `find -type d`.split($/).map {|f| Rails.root.join(f) }
Upvotes: 0