Heroku ActionController::RoutingError (uninitialized constant error) - ruby-on-rails

Rails 4.2.4, Ruby 2.1.7
I have a module inside lib/ directory.
lib/BLL/user_feed.rb
module BLL
class UserFeed
def initialize
logger.debug "Class has been initialized"
end
def get_user_feed(user_id)
# logic here
return {
# object
}
end
end
end
When I try to include that in my controller to use my user_Feed logic,
class UserfeedController < ApplicationController
include BLL
before_action :authenticate_user!
def show
# some logic
end
end
In my config/application.rb
config.autoload_paths << Rails.root.join('lib')
This runs fine locally, however, it breaks when I deploy it on Heroku.
it's throwing
ActionController::RoutingError (uninitialized constant UserfeedController::BLL):
error.
2015-10-20T13:45:13.791457+00:00 app[web.1]: /app/app/controllers/api/v1/userfeed_controller.rb:1:in `<top (required)>': uninitialized constant Bll (NameError)
2015-10-20T13:45:13.791457+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.1.0/gems/railties-4.2.4/lib/rails/engine.rb:472:in `block (2 levels) in eager_load!'
2015-10-20T13:45:13.791458+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.1.0/gems/railties-4.2.4/lib/rails/engine.rb:471:in `each'
2015-10-20T13:45:13.791459+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.1.0/gems/railties-4.2.4/lib/rails/engine.rb:471:in `block in eager_load!'
2015-10-20T13:45:13.791460+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.1.0/gems/railties-4.2.4/lib/rails/engine.rb:469:in `each'
2015-10-20T13:45:13.791462+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.1.0/gems/railties-4.2.4/lib/rails/engine.rb:469:in `eager_load!'
2015-10-20T13:45:13.791463+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.1.0/gems/railties-4.2.4/lib/rails/engine.rb:346:in `eager_load!'
Any suggestions?

I think you're missing a module BLL; end in lib/bll.rb
But also, play with naming the module Bll, but I don't think that's it

try
config.autoload_paths += %W(#{config.root}/lib/BLL)
and dont forget to restart the server
Edit1
Moreover; changing the name of Dir BLL to bll also works

Ruby defaults to CamelCase, you'll have to use the following:
#vendor/bll/user_feed.rb
module Bll
class UserFeed
...
end
end
As a second, the vendor dir is autoloaded (to the best of my knowledge), so the above code should work to fix the UnrecognizedConstant error.
https://softwareengineering.stackexchange.com/questions/123305/what-is-the-difference-between-the-lib-and-vendor-folders

Rails in fact loads your app directory when the app loads. So actually no need to mention your app/bll in autoload paths.
However, what I am doing wrong here in this case is adding module on top of the class.
So, my app is looking for app/bll/bll/Whatever
module Bll - there is not need for this module to be declared
class Whatever
# some logic.
end
end
All you need to do is.
class Whatever
end
After this, your class is available to use.

Related

How do I load a module/class from my config/application.rb file in Rails 4?

I’m using Rails 4.2.3. I’ve created a file at lib/app_config/aws_secrets.rb, which looks like
require 'yaml'
module mycoAppConfig
class AwsSecrets
def self.load
…
end
end
end
Then in my config/application.rb file, I have invoked the above method using
AppConfig::AwsSecrets.load
But when I run a rake task, I’m getting this uninitialized constant error
NameError: uninitialized constant MycoWeb::Application::AppConfig
/Users/myuser/Documents/workspace/myco/myapp/config/application.rb:120:in `block in <class:Application>'
/Users/myuser/.rvm/gems/ruby-2.4.5#myapp/gems/activesupport-4.2.10/lib/active_support/lazy_load_hooks.rb:36:in `execute_hook'
/Users/myuser/.rvm/gems/ruby-2.4.5#myapp/gems/activesupport-4.2.10/lib/active_support/lazy_load_hooks.rb:28:in `block in on_load'
/Users/myuser/.rvm/gems/ruby-2.4.5#myapp/gems/activesupport-4.2.10/lib/active_support/lazy_load_hooks.rb:27:in `each'
/Users/myuser/.rvm/gems/ruby-2.4.5#myapp/gems/activesupport-4.2.10/lib/active_support/lazy_load_hooks.rb:27:in `on_load'
/Users/myuser/.rvm/gems/ruby-2.4.5#myapp/gems/railties-4.2.10/lib/rails/railtie/configuration.rb:53:in `before_configuration'
/Users/myuser/Documents/workspace/myco/myapp/config/application.rb:113:in `<class:Application>'
/Users/myuser/Documents/workspace/myco/myapp/config/application.rb:9:in `<module:mycoWeb>'
What’s the proper way to include/name my library?
according to the naming convention you should use module name as AppConfig
require 'yaml'
module AppConfig
class AwsSecrets
def self.load
…
end
end
end
application.rb
config.eager_load_paths += %W( #{config.root}/lib/**/*.rb )

Unable to autoload constant

In rails 5.2 I have a small lib
which is under app/lib/itunes (everything under app/lib should be autoloaded right?).
Nevertheless I get a load error when I start sidekiq
LoadError: Unable to autoload constant Itunes::ItunesClient,
expected /app/lib/itunes/itunes_client.rb to define it
2018-06-06T19:38:49.560Z 46606 TID-ov5d572iq WARN:
.rvm/gems/ruby-2.5.0/gems/activesupport-5.2.0/lib/active_support/dependencies.rb:503:in `load_missing_constant'
.rvm/gems/ruby-2.5.0/gems/activesupport-5.2.0/lib/active_support/dependencies.rb:193:in `const_missing'
/app/interactors/fetch_itunes_app_service.rb:4:in `call'
class FetchItunesAppService
include Interactor
def call
#client = Itunes::ItunesClient.new
...
end
end
#app/lib/itunes/itunes_client.rb
class Itunes::ItunesClient < Itunes::ItunesBaseClient
...
end
#app/lib/itunes/itunes_base_client.rb
class Itunes::ItunesBaseClient
...
end

Using a non-ActiveRecord model, can't access it through rails console

(Rails 4.2.4) Hello, beginner here. I am working on a project which does not need a DB or activeRecord. Therefore, when making my Rails project I appended the -O (to disable Active Record and database) (rails new MyApp -O)
I read that to do a model not backed by a database you can just create a file in
app/models/site.rb.
No need to do:
rails generate model Site
So I added my model, which looks something like this:
class Site
attr_reader :name
attr_reader :out_average
attr_reader :in_average
attr_reader :change
def initialize(name, in_average, out_average)
#name = name
#out_average = out_average
#in_average = in_average
#change = find_increase
end
def find_increase()
if #in_average && #out_average != 0
#change = ((#in_average - #out_average)/#out_average)*100
else
#change = 0
end
return #change
end
end
So, I then started up console "rails c" and when I try to invoke a new Site object, I get an error:
irb(main):001:0> Site.new
NameError: uninitialized constant Site
from (irb):1
from /home/ms-87/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/railties-4.2.4/lib/rails/commands/console.rb:110:in `start'
from /home/ms-87/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/railties-4.2.4/lib/rails/commands/console.rb:9:in `start'
from /home/ms-87/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/railties-4.2.4/lib/rails/commands/commands_tasks.rb:68:in `console'
from /home/ms-87/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/railties-4.2.4/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from /home/ms-87/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/railties-4.2.4/lib/rails/commands.rb:17:in `<top (required)>'
from /home/ms-87/Documents/projects/rails_projects/site_seasonality/bin/rails:8:in `<top (required)>'
from /home/ms-87/.rbenv/versions/2.2.3/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from /home/ms-87/.rbenv/versions/2.2.3/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from -e:1:in `<main>'
I made sure I started the console from the root of my app. I also made sure to use the proper naming convention (site.rb is the filename in app/model/, "Site" is the name of my class inside the file). Can anyone help me as to why this isn't working? Is my thinking here wrong? Thanks.
My first error was that my filenames were capitalized "Site.rb", I had actually fixed this before I posted. But after I fixed it, I accidentally started using "irb" instead of "rails c". DOH! Sorry for the post.

How do I implement a decorator in my app?

I am trying to implement this in my app.
The article says that I must create a decorator - but it doesn't go into much detail about exactly how to do that. This is the code:
module CartDecorator
extend ActiveSupport::Concern
module InstanceMethods
def is_downloadable?
items = self.items.collect { |li| li[:variant].item }
items.all? { |i| i.is_downloadable }
end
def has_downloadable?
items = self.items.collect { |li| li[:variant].item }
items.any? { |i| i.is_downloadable }
end
end
end
Piggybak::Cart.send(:include, CartDecorator)
I am not sure if I should add that code to some model.rb (for what it's worth, I don't have a piggybak_cart.rb in my app/models/ folder).
I tried running rails g decorator Cart and that didn't work.
What I have done is put the above code in app/helpers/cart_helper.rb.
Then when I tried to run a rails g command (for something else), I am now getting this error:
/.rvm/gems/ruby-2.0.0-p0#myapp/gems/activesupport-3.2.13/lib/active_support/inflector/methods.rb:230:in `block in constantize': uninitialized constant CartHelper (NameError)
from /.rvm/gems/ruby-2.0.0-p0#myapp/gems/activesupport-3.2.13/lib/active_support/inflector/methods.rb:229:in `each'
from /.rvm/gems/ruby-2.0.0-p0#myapp/gems/activesupport-3.2.13/lib/active_support/inflector/methods.rb:229:in `constantize'
from /.rvm/gems/ruby-2.0.0-p0#myapp/gems/activesupport-3.2.13/lib/active_support/core_ext/string/inflections.rb:54:in `constantize'
from /.rvm/gems/ruby-2.0.0-p0#myapp/gems/actionpack-3.2.13/lib/abstract_controller/helpers.rb:136:in `block in modules_for_helpers'
from /.rvm/gems/ruby-2.0.0-p0#myapp/gems/actionpack-3.2.13/lib/abstract_controller/helpers.rb:131:in `map!'
What's the best way to approach this?
You should add the above code into app/decorators/cart_decorator.rb
the decorators folder will be new and be autoloaded when you start your rails app. when this is run Piggybak::Cart.send(:include, CartDecorator) it will decorate your Piggybag::Cart with the methods you declared above.

using custom classes in rails console

I create a custom class which I call MyClass in a module MyModule
module MyModule
class MyClass
def initialize
... # Some code here
end
end
end
I save this code in a file called mymodule.rb
I place this file in the lib directory of my rails application, and add the following line to my application.rb
config.autoload_paths += %W(#{config.root}/lib)
When I fire up the rails console and try to use this file. it just doesnt work.
m = MyModule::MyClass.new()
NameError: uninitialized constant MyModule
from (irb):1
from /Users/matt/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.6/lib/rails/commands/console.rb:44:in `start'
from /Users/matt/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.6/lib/rails/commands/console.rb:8:in `start'
from /Users/matt/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.6/lib/rails/commands.rb:23:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
What do I need to do in order to be able to use that class in the rails console
Any help appreciated
Name the file my_class.rb and place it in the directory my_module. That should fix your problem.

Resources