为模型/视图/控制器/邮件程序创建 Rails 生成器
我有一个很大程度上基于表单的应用程序。许多形式的构造完全相同,因此它似乎很适合生成器。我想做的是创建一个像这样工作的(虚构的例子):
rails g request_form name:string phone:string date_of_birth:date
在任何情况下,标准的、空的控制器、帮助器、模型等等都不太行。我已经阅读了 Rails 代码,但坦率地说,它对解决这个问题并没有多大帮助。我具体想做的是:
- 根据命令行上指定的字段创建模型和迁移
- 基于我的模板 创建控制器和帮助器 基于我的模板
- 创建视图
- 创建空规格
- 基于我的模板创建邮件程序
- 根据我的模板创建邮件程序视图
我在第 1 方停滞不前:我到底如何获取rails g 命令的 ARGV 部分——即字段名称?然后是第 2 步:如何在适当的情况下连接到内置生成器,在不适合的情况下填写我自己的东西?
这类似于,
rails g scaffold blah:type blah1:type
所以我不认为这比我能咀嚼的更多……
任何帮助,非常感谢!
I have an app that's heavily form-based. Many of the forms are constructed exactly alike so it seems like a natural fit for a generator. What I want to do is create one that works like this (fictitious example):
rails g request_form name:string phone:string date_of_birth:date
In any case, the standard, empty controllers, helpers, models, and so on won't quite do. I've read the Rails code but it's, frankly, not been a heck of a lot of help in puzzling this out. What I want to do specifically is:
- Create a model and migration given the fields specified on the command line
- Create a controller and helper, based on my template
- Create views based on my templates
- Create empty specs
- Create a mailer based on my template
- Create mailer views based on my templates
I'm getting stalled at square 1: How the heck do I get the ARGV part of the rails g command -- that is, the field names? Then there's square 2: How do I hook into the built-in generators, where appropriate and fill in my own stuff where not?
This is analogous to
rails g scaffold blah:type blah1:type
so I don't think this is biting off more than I can chew...
Any help mucho appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这个伟大的宝石所需的所有灵感:https://github.com/ryanb/nifty-generators
All inspiration needed in this great gem: https://github.com/ryanb/nifty-generators
很棒的 Ryan Bates 有一个关于在 Rails 3 中编写生成器的截屏视频,你看过吗?
The awesome Ryan Bates has a screencast on writing generators in Rails 3, have you watched that?