创建生成器Rails

时间:2012-12-01 04:37:12

标签: ruby-on-rails ruby generator

我在使用rails创建生成器时出现问题,这是我遇到的错误。 我正在尝试创建一个生成器,通过提供商Twilio创建SMS验证。

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

这是我的代码

 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

2 个答案:

答案 0 :(得分:0)

重新启动Rails服务器,然后尝试

如需帮助,请参阅以下链接:Rails Adding Twilio Sub Accounts : Uninitialized constant User::Twilio

答案 1 :(得分:0)

我不能肯定地说,但这通常不是我在宝石中看到生成器的方式。通常它们与实际的gem_name.rb文件分开。我想如果你将生成器逻辑移动到更常见的lib/generators/generator_name/generator_name.rb,你会有更多的运气。然后,您可以将要复制的文件放在子目录../generator_name/templates/

另请注意,您需要将.rb结尾添加到copy_file "verifications_controller,"

要使模板解决问题,您可以按照我编写的here使用生成器的简单gem的布局。