在 Rails 生成器中强制使用 template() 覆盖文件?
我自己有一个生成器,作为一组其他操作的一部分,需要设置一个包含一堆 mixins、默认值、注释等的模型类。
我想使用相同的rails g 模型.. . 代码(我从我的生成器调用 invoke
),但问题是存在冲突,因为我的模板和模型生成器的模板试图互相冲突:
$ be rails g entry_form karaoke events full_name:string group_name:string
create app/controllers/karaokes_controller.rb
create app/views/karaokes/show.html.erb
create app/views/karaokes/thanks.html.erb
route resource :karaoke
create app/models/karaoke_entry.rb
invoke active_record
create db/migrate/20111004004008_create_karaoke_entries.rb
conflict app/models/karaoke_entry.rb
Overwrite app/models/karaoke_entry.rb?
(enter "h" for help) [Ynaqdh]
有什么建议如何解决这个问题吗?
(我想出的最好办法就是将我的模型文件创建移到底部,并找到某种方法来强制 template
/ copy_file
继续并覆盖文件而不打扰用户,但我看不到任何预先存在的方法。)
I have myself a generator that, as part of a set of other operations, needs to set up a model class with a bunch of mixins, defaults, comments, etc.
I want to be using the same rails g model ...
code (I'm calling invoke
from my generator), but the problem is that there's a conflict because my template and the model generator's template are trying to splat each other:
$ be rails g entry_form karaoke events full_name:string group_name:string
create app/controllers/karaokes_controller.rb
create app/views/karaokes/show.html.erb
create app/views/karaokes/thanks.html.erb
route resource :karaoke
create app/models/karaoke_entry.rb
invoke active_record
create db/migrate/20111004004008_create_karaoke_entries.rb
conflict app/models/karaoke_entry.rb
Overwrite app/models/karaoke_entry.rb?
(enter "h" for help) [Ynaqdh]
Any recommendations how to get around this?
(The best I've come up with is to maybe move my model file creation to the bottom, and find some way to force template
/ copy_file
to go ahead and overwrite the file without bothering the user, but I can't see any pre-existing way of doing this.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将
force: true
或skip: true
附加到模板调用中:Append
force: true
orskip: true
to the template call:一些小组集思广益找到了解决这个问题的方法。
您无法覆盖(据我所知),但您可以告诉模型生成器跳过已经存在的文件。这有效:(
我仍然欢迎一种允许
template
覆盖文件的方法。)Some group brainstorming turned up a way to work around this problem.
You can't override (in so far as I can tell), but you can tell the model generator to skip files that already exist. This works:
(I would still welcome a way to allow
template
to override files.)