第一种:用生成器
最正确:Rails::Generators::HelperGenerator
config.generators.scaffold_controller = ntroller
①生成rails g generator rails/Ntroller
②添加
/home/pinmi/bababa/lib/generators/rails/ntroller/ntroller_generator.rb
class Rails::NtrollerGenerator < Rails::Generators::NamedBase
source_root File.expand_path("../templates", __FILE__)
def copy_initializer_file
copy_file "tmp1.rb","app/controllers/#{file_name}_controller.rb"
end
end
新建
/home/pinmi/bababa/lib/generators/rails/ontroller/templates/tmp1.rb
Controller
③修改
/home/pinmi/bababa/config/application.rb
module Bababa
class Application < Rails::Application
config.generators do |g|
g.orm :active_record
g.template_engine :erb
g.test_framework :test_unit, fixture: false
g.stylesheets false
g.javascripts false
g.scaffold_controller :ntroller
end
end
end
查看所有生成器
rails generate generator Good
class GoodGenerator < Rails::Generators::NamedBase
def create_initializer_file
create_file "config/initializers/#{file_name}.rb", "# Add initialization content here"
end
end
rails g good --help
rails g good Initializer
pinmi@pinmi-desktop:~/bababa$ rails g good Initializer
create config/initializers/hello.rb
rails g good Why
pinmi@pinmi-desktop:~/bababa$ rails g good Why
create config/initializers/hello.rb
第二种:直接用模板,再lib下新建templates/erb/scaffold/_form.html