I have a rails application in that I have modules inside /app/adapters/UDB/ folder. The module is not loading. I have added the following in application.rb
config.autoload_paths += Dir["#{config.root}/app/adapters/**/*"]
I am calling module from model file /models/userinvite.rb
def update_cassandra
ypusers = UDB::YpRewards.new.ypusers
ypusers.execute("UPDATE invitation_backlog SET invitation_code = '#{invitation_code}', invitation_sent_date = #{invitation_sent_date.to_i * 1000}, invited_by = '#{invited_by}' WHERE email_address = '#{email}'")
end
/app/adapters/UDB/yp_rewards.rb
module UDB
class YpRewards
def initialize
end
def ypusers
#ypusers ||= UDB::Connection.new.connection.connect('ypusers')
end
...
Please help me solving it.
I think the issue is with module name.
Your module name is UDB, then you can load this module by specifying its name in smallcase letters as per rails naming convention (camelcasing)
config.autoload_paths += %W( #{config.root}/app/adapters/u_d_b)
Try including the module in your UserInvite model,
include UDB
This is a good site to know more about the placement and usage of modules.
Related
I've created directory for API logic: app/api/EtherumAPI/V1 and put there following code:
module EtherumAPI
module V1
class Request
class << self
def trancation
end
end
end
end
end
Registered in my application.rb:
config.autoload_paths << "#{Rails.root}/app/api"
config.eager_load_paths << "#{Rails.root}/app/api"
And try to call it inside my controller:
def index
#test = EtherumAPI::V1::Request
#test.trancation
end
But I got this error:
uninitialized constant HomeController::EtherumAPI
I tried also something like "include EtherumAPI::V1" but it didn't succeed as well. How can I fix it and be able to call methods from Request class?
First off you can get rid of:
config.autoload_paths << "#{Rails.root}/app/api"
config.eager_load_paths << "#{Rails.root}/app/api"
All the subdirectories of app are by default auto-load paths. Occasionally the auto-loader gets "stuck" and will not pick up newly added directories. You can usually fix that by restarting the rails server and spring ($ spring stop).
There are two issues at play here. The first is inflection. Rails inflects file names from classes by camelizing the class name. Unfortunately this does not work automatically for acronyms as ABC -> a_b_c.rb.
So for the autoloader to look for EtherumAPI in etherum_api.rb you need to add an inflection:
# config/initializers/inflections.rb
ActiveSupport::Inflector.inflections(:en) do |inflect|
inflect.acronym 'EtherumAPI'
end
The second issue is that the module names must match the actual path to the file.
# app/api/etherum_api/v1/request.rb
module EtherumAPI
module V1
class Request
class << self
def trancation
end
end
end
end
end
I have the following in my controller:
class SurveysController < ApplicationController
def index
survey_provider = FluidSurveysProviders::SurveyProvider.new
contact_lists = survey_provider.get_lists()
#survey = Survey.new(contact_lists)
end
And I'm receiving this error:
NameError in SurveysController#index
uninitialized constant SurveysController::FluidSurveysProviders
Excuse my Rails noobiness, I'm sure I'm leaving out something important here. But it seems to me that I am trying to "initialize" the constant with this line:
survey_provider = FluidSurveysProviders::SurveyProvider.new
But that's the same line that's throwing an error because it's not initialized. Where should I be "initializing" the Provider?
Once you require fluid_surveys_providers (or similar) then do this:
include FluidSurveysProviders
Make sure SurveyProvider is wrapped with module FluidSurveysProviders. It may look like this
module FluidSurveysProviders
class SurveyProvider
...
end
end
if its an ActiveRecord object try this
class FluidSurveysProviders::SurveyProvider < ActiveRecord::Base
...
end
The SurveyProvider was not loaded correctly.
For a quick fix, move the class file into app directory, e.g. app/lib/survey_provider.rb. Then all code inside app will be auto-loaded by Rails.
Or make sure the path to class SurveyProvider is included in the autoload_path of Rails. In config/application.rb
config.autoload_paths += %W(#{config.root}/lib) # where lib is directory to survery_provider
If you use Rails 5, be careful that autoload is disabled in production environment. Check this link for more info.
I am getting the following error, when I try to run my application:
uninitialized constant RegistrationsController::User_serial
In my config/application.rb, I have:
config.autoload_paths += %W(#{config.root}/lib)
config.autoload_paths += Dir["#{config.root}/lib/**/"]
In my registrations_controller.rb, I have the following:
class RegistrationsController < Devise::RegistrationsController
........
def create
#user = User.new(params[:user])
user_serial_local = User_serial.new #initialize class defined in lib/my_tools.rb
date_time_local = Date_formatter.new
......
In lib/my_tools.rb, I define some classes:
class User_serial
def self.calculate(first,last)
first_3 = first[0..2]
last_4 = last[0..3]
time = Time.now.to_i
return first_3 + last_4 + time.to_s
end
end
class Date_formatter
def self.datetime
return Time.now.strftime("%Y-%m-%d %H:%M:%S")
end
end
There are a lot of references to overriding a class, and instructions on how to insure that anything placed in the lib folder is included (followed in my code). Why am I getting the error message?
For rails' magic loading to work it needs to be able to find the file a class/module is defined in based only on the class name.
This in turn means sticking to rails' naming conventions and putting things where rails expects: UserSerial should be defined in user_serial.rb. You might be able to get User_serial to work as a class name but rails will never look in my_tools.rb for that class.
I just created a module location.rb inside /lib folder with following contents:
module Location
def self.my_zipcode()
zip_code = "11215"
end
end
And now in my controller i am trying to call "my_zipcode" method:
class DirectoryController < ApplicationController
def search
require 'location'
zip_code = Location.my_zipcode()
end
end
But it throws an error:
undefined method `my_zipcode' for Location:Module
You can also add the following to your config/application.rb
config.autoload_paths += %W(#{config.root}/lib)
And it should autoload your module without having to restart rails.
You might have to restart the rails server for it to recognize stuff in the lib directory.
I have a class that I'm trying to use in my controller in the index action.
To simplify it, it looks like this
class PagesController < ApplicationController
def index
#front_page = FrontPage.new
end
end
FrontPage is a class that I have defined. To include it, I have placed it in the /lib/ folder. I've attempted to require 'FrontPage', require 'FrontPage.rb', require 'front_page', and each of those with the path prepended, eg require_relative '../../lib/FrontPage.rb'
I keep getting one of the following messages: cannot load such file -- /Users/josh/src/ruby/rails/HNReader/lib/front_page or
uninitialized constant PagesController::FrontPage
Where do I put this file/how do I include it into a controller so that I can instantiate an object?
This is Rails 3.1.3, Ruby 1.9.2, OS X Lion
You should be able to use require 'front_page' if you are placing front_page.rb somewhere in your load path. I.e.: this should work:
require 'front_page'
class PagesController < ApplicationController
def index
#front_page = FrontPage.new
end
end
To check your load path, try this:
$ rails console
ree-1.8.7-2011.03 :001 > puts $:
/Users/scottwb/src/my_app/lib
/Users/scottwb/src/my_app/vendor
/Users/scottwb/src/my_app/app/controllers
/Users/scottwb/src/my_app/app/helpers
/Users/scottwb/src/my_app/app/mailers
/Users/scottwb/src/my_app/app/models
/Users/scottwb/src/my_app/app/stylesheets
# ...truncated...
You can see in this example, the first line is the project's lib directory, which is where you said your front_page.rb lives.
Another thing you can do is add this in your config/application.rb:
config.autoload_paths += %W(#{config.root}/lib)
That should make it so you don't even need the require; instead Rails will autoload it then (and everything else in your lib dir, so be careful).
The file was named FrontPage.rb. Changing the name to 'front_page.rb', but leaving the class name as 'FrontPage' resolved the issue.
We just need to load the file,
class PagesController < ApplicationController
require 'front_page.rb'
def index
#front_page = FrontPage.new
end
end
lib/front_page.rb
class FrontPage
end
We can also set the application.rb to autoload these files
# Custom directories with classes and modules you want to be autoloadable.
# config.autoload_paths += %W(#{config.root}/extras)
Second option would be a preferable solution.