我有一个看起来像这样的基本自定义生成器,它继承自 Rails 5.1 应用程序中的 Rails::Generators::NamedBase
class NotificationGenerator < Rails::Generators::NamedBase
source_root File.expand_path('../templates', __FILE__)
def notification
copy_file "notification.rb", "app/notifications/#{file_name}.rb"
copy_file "notification_spec.rb", "spec/notifications/#{file_name}_spec.rb"
end
end
我的模板文件名为 notification.rb.tt,它位于 ../templates 目录下。
模板如下所示:
class <%= class_name %> < Notification
def to_mail
end
def to_sms
end
end
但是,当我运行生成器时,创建的文件在文件中有 <%= class_name %> 而不是该方法调用的结果。如何让生成器像 erb 模板一样实际渲染?