I am having a problem creating a generator in rails this is the error I am getting.
I am trying to create a generator that creates does SMS verification through the provider Twilio.
require "sms_auth-twilio"
NameError: uninitialized constant Twilio::Rails
from /Users/sherodtaylor/.rvm/gems/ruby-1.9.3-p125/gems/sms_auth-twilio-0.0.2/lib/sms_auth-twilio.rb:3:in `<module:Twilio>'
from /Users/sherodtaylor/.rvm/gems/ruby-1.9.3-p125/gems/sms_auth-twilio-0.0.2/lib/sms_auth-twilio.rb:2:in `<top (required)>'
from /Users/sherodtaylor/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:60:in `require'
from /Users/sherodtaylor/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:60:in `rescue in require'
from /Users/sherodtaylor/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:35:in `require'
from (irb):1
from /Users/sherodtaylor/.rvm/rubies/ruby-1.9.3-p125/bin/irb:16:in `<main>'
This is my code
require "sms_auth-twilio/version"
module Twilio
class TwilioGenerator < Rails::Generator::Base
source_root File.expand_path("../generator", __FILE__)
def add_twilio_initializer
copy_file "twilio.rb", "config/initializer/twilio.rb"
end
# config info for the API
def twilio_config_info
copy_file "twilio.yml", "config/twilio.yml"
end
# Verification Controller to verify the company
def copy_verifications
copy_file "verifications_controller", "app/contoller/verifications_controller.rb"
end
end
end
Restarting your Rails server and then try
For your Help see this link : Rails Adding Twilio Sub Accounts : Uninitialized constant User::Twilio
I can't say for sure, but this is not typically how I see generators created within a gem. Usually they are separate from the actual gem_name.rb file. I think if you move the generator logic into the more common lib/generators/generator_name/generator_name.rb you'll have more luck. And then you can place the files to be copied within a subdir ../generator_name/templates/
Also note you need to add the .rb ending to your copy_file "verifications_controller,"
For a template to work off, you can follow the layout of a simple gem I wrote here which utilizes a generator.
Related
I am trying to upgrade a Rails 6.1.3.1 application from Ruby 2.6.6 to 3.0.0
All the rspec tests run fine and the in development everything seems to work just fine except one thing:
Even the simplest of tasks fails with this error:
"class variable ##debug_missing_translation of ActionView::Base is overtaken by Object"
For example, this simpletask.rake file
task simpletask: :environment do
puts 'Hello'
end
is not able to run as I get the following error:
lxxx#xxx:~/Workspace/edumino$ rails simpletask
rails aborted!
class variable ##debug_missing_translation of ActionView::Base is overtaken by Object
/home/xxx/railsapp/config/environment.rb:5:in `<top (required)>'
/home/xxx/railsapp/bin/rails:5:in `require'
/home/xxx/railsapp/bin/rails:5:in `<top (required)>'
/home/xxx/railsapp/bin/spring:10:in `require'
/home/xxx/railsapp/bin/spring:10:in `block in <top (required)>'
/home/xxx/railsapp/bin/spring:7:in `<top (required)>'
Tasks: TOP => simpletask => environment
(See full trace by running task with --trace)
...so, it turns out the problem was an already existing task. I don't remember exactly why this was necessary but the task had this:
# frozen_string_literal: true
require "#{Rails.root}/app/helpers/cron_helper"
require "#{Rails.root}/app/helpers/notifier_helper"
require "#{Rails.root}/app/helpers/application_helper"
include ApplicationHelper
include ActionView::Helpers::TranslationHelper
include CronHelper
namespace :cron do
task sms: :environment do |_t, _args|
...
end
end
I assume that the guilty code was including the ActionView::Helpers::TranslationHelper
Again, I can't remember why I had to do that at that time. But if you bump into this kind of 'overtaken by' error when upgrading to Ruby 3.0, check your other tasks and see what kinda weird includes you might have had. In my case, since I didn't need the old task, I just removed it.
In rails 4 app, I am trying to create one rake task. I am trying to include a module feature for it but its not working.
Module file is (/app/models/concerns/user/tags.rb),
module Concerns::User::Tags
extend ActiveSupport::Concern
...
end
Rakefile is (/lib/tasks/keywords.rake),
require "#{Rails.root}/app/models/concerns/user/tags.rb"
include Concerns::User::Tags
namespace :keywords do
desc 'Add data'
task :add => :environment do
puts "Adding"
end
end
When I run this bundle exec rake keywords:add -t getting an error like,
NameError: uninitialized constant Concerns
/vagrant/app/models/concerns/user/tags.rb:1:in `<top (required)>'
/vagrant/lib/tasks/keywords.rake:1:in `<top (required)>'
How can I solve this issue? Please help me.
I think you can try moving the include Concerns::User::Tags inside the task block. If you need the module functionality in several tasks then yes include it in every task. You problem is essentially discussed here Is it possible to include modules in rake task and make its methods available for the task in rails app?
(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.
I have the following script:
#!/usr/bin/ruby
require 'rubygems' unless defined?(Gem)
require 'mongoid'
include Mongoid::Document
#classes = Availability.where(:availability_date.gt => Time.now.utc + 1.hours).to_a
puts #classes.count
But I always get:
classes_notification.rb:6:in `': uninitialized constant Availability (NameError)
Some help please, I need to make a daemon to send emails with the information on my availability model.
Thanks in advance.
UPDATE CODE
my script is under app/script/user/remeber_classes.rb
how can I access the require File.dirname(FILE) + "/../../config/environment"
because I'm getting
/Users/jeanosorio/.rvm/rubies/ruby-1.9.3-p484/lib/ruby/site_ruby/1.9.1/rubygems/core_ext/kernel_require.rb:126:in
require': cannot load such file -- ./../../config/environments
(LoadError) from
/Users/jeanosorio/.rvm/rubies/ruby-1.9.3-p484/lib/ruby/site_ruby/1.9.1/rubygems/core_ext/kernel_require.rb:126:in
require' from remember_class.rb:5:in `'
you must include rails environment first
add require "path_to_the_application/config/environment" to your code
or
you can always access the database directly using mongo gem
see this
https://github.com/mongodb/mongo-ruby-driver/wiki/Tutorial
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.