Rails3:在自定义生成器中生成模型

时间:2011-01-03 22:37:51

标签: ruby-on-rails

嘿伙计们,我正在Rails 3.0.3中编写一个自定义生成器(名为shcaffold),我希望它根据传入它的第一个参数生成一个active_record模型(和迁移)(模型的名称) )。

但是,当我运行命令时,我收到此错误:

$ rails g shcaffold someclass
   error  active_record [not found]

这是我的生成器定义,存储在lib / generators / shcaffold / shcaffold_generator.rb中:

class ShcaffoldGenerator < Rails::Generators::NamedBase
   include Rails::Generators::ResourceHelpers
   source_root File.expand_path('../templates', __FILE__)

   # Run Other Generators
   hook_for :model, :in => :rails, :required => true
end 

我在应用程序的application.rb中定义了orm:

config.generators do |g|
  g.orm             :active_record
  g.template_engine :erb
  g.test_framework  :test_unit, :fixture => false
  g.stylesheets     false
end  

但是,唉,我没有运气。

2 个答案:

答案 0 :(得分:0)

没有详细解答,但你应该从中获取灵感:https://github.com/ryanb/nifty-generators

答案 1 :(得分:0)

关于幽冥地区error active_record [not found]疼痛的另一个类似问题=)

在这里:Rails: hook_for :orm not finding active_record

如何解决:

  1. 保持在Rails :: Generators命名空间

    这已经解决了大部分未发现的问题。

  2. 将您的生成器配置为默认值。 (Generators模型空间内的任何地方)

    module Rails
      module Generators
        class Railtie < ::Rails::Engine
            if config.respond_to?(:app_generators)
                config.app_generators.orm = :my_own_model
             else
                config.generators.orm = :my_own_model
             end
          end
       end
    end
    
  3. 你不再有rails g的麻烦了。

    的nJoy!

相关问题