在Rails 5中覆盖默认的Jbuilder脚手架生成器

时间:2017-02-22 01:02:53

标签: ruby-on-rails jbuilder

我已经在Override default scaffold generator in rails 3中使用jackpipe的说明为控制器创建了脚手架生成器,但我没有设法对Jbuilder执行相同的操作。

我试过了:

#/config/application.rb
config.generators do |g|
  g.scaffold_controller = "admin_scaffold_controller"
  g.jbuilder = "admin_jbuilder"
end

和这个

#/lib/generators/rails/admin_jbuilder/admin_jbuilder_generator.rb
require 'lib/generators/rails/jbuilder_generator'
module Rails
  module Generators
    class AdminJbuilderGenerator < JbuilderGenerator # :nodoc:

      def create_root_folder
        path = File.join('app/views/admin', controller_file_path)
        empty_directory path unless File.directory?(path)
      end

      def copy_view_files
        %w(index show).each do |view|
          filename = filename_with_extensions(view)
          template filename, File.join('app/views/admin', controller_file_path, filename)
        end
        template filename_with_extensions('partial'), File.join('app/views/admin', controller_file_path, filename_with_extensions("_#{singular_t$
      end
    end
  end
end

但我有这个:

[WARNING] Could not load generator "generators/rails/admin_jbuilder/admin_jbuilder_generator". Error: uninitialized constant Rails::Generators::JbuilderGenerator.

1 个答案:

答案 0 :(得分:0)

require调用未指向正确的文件,而不是:

require 'lib/generators/rails/jbuilder_generator'

应该是:

require 'generators/rails/jbuilder_generator'

请注意应删除的额外lib部分。