覆盖 Rails 脚手架生成器 [英] Override Rails scaffold generator

查看:40
本文介绍了覆盖 Rails 脚手架生成器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

rails 中的脚手架生成器从位于 lib/rails/generators/erb/scaffold/templates railties 路径和 lib/templates 中的模板生成 MVC 层项目中的文件夹.

如果您将原始文件从 railties 文件夹复制到您的项目 lib 文件夹,那么您将能够自定义原始 scaffold 生成器,正如 Daniel Fone 在他的

原始scaffold_generator.rb 位于~/.rvm/gems/ruby-2.1.0/gems/railties-4.2.4/lib/rails/generators/erb/脚手架.要添加新文件 (_info.html.erb),我们将其添加到 available_views 方法中.

def available_views%w(索引编辑显示新的 _form _info)结尾

希望对某人有所帮助.

The scaffold generator in rails generates the MVC layers from the templates located in lib/rails/generators/erb/scaffold/templates railties path and from the lib/templates folder in your project.

If you copy the original files from the railties folder to your projects lib folder, then you'll be able to customize the original scaffold generator, as Daniel Fone explain in his blog.

The original Rails generator uses 5 files in the views layer:

  • _form.html.erb
  • edit.html.erb
  • index.html.erb
  • new.html.erb
  • show.html.erb

I was wondering if the there is a way to extend this behavior by adding another file to the set. like _header.html.erb or _info.html.erb (some partials that I've design).

解决方案

Well after a couple months I've found the solution. Instead of creating a new generator as the Rails documentation states, I override the default generator but in my projects lib folder.

The original scaffold_generator.rb is located at ~/.rvm/gems/ruby-2.1.0/gems/railties-4.2.4/lib/rails/generators/erb/scaffold. To add the new file (_info.html.erb) we will add it to the available_views method.

def available_views
    %w(index edit show new _form _info)
end

Hope it helps someone.

这篇关于覆盖 Rails 脚手架生成器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆