Override default scaffold generator in rails 3

前端 未结 5 1267
一生所求
一生所求 2020-12-30 05:28

I\'ve created a generator for a controller in rails 3. Now I want to use this generator as the default generator when using the scaffolding generator.

Is that possib

5条回答
  •  隐瞒了意图╮
    2020-12-30 06:14

    If you simply want to use your own controller template, you can just put it in lib/templates/rails/scaffold_controller/controller.rb

    If you want to replace the scaffold_controller_generator code itself, for example, so that the controller scaffold generates additional class files. you can create lib/generators/rails/my_scaffold_controller/my_scaffold_controller_generator.rb with templates under lib/generators/rails/my_scaffold_controller/templates.

    Remember to point rails at your new scaffold_controller in config/application.rb:

    config.generators do |g|
      g.scaffold_controller = "my_scaffold_controller"
    end
    

    For my_scaffold_controller_generator.rb you could copy from the railties gem under railties-3.x.x/lib/rails/generators/rails/scaffold_controller if you want to modify default behaviour, or inherit from it if you just want to add functionality:

    require 'rails/generators/rails/scaffold_controller/scaffold_controller_generator'
    
    module Rails
      module Generators
        class MyScaffoldControllerGenerator < ScaffoldControllerGenerator
          source_root File.expand_path("../templates", __FILE__)
    
          def new_funtionality
          end
    
        end
      end
    end
    

提交回复
热议问题