【发布时间】:2012-12-15 18:17:21
【问题描述】:
我正在尝试制作简单的视图生成器并使用 DRY 原则,我不想拥有自己的 html (erb/haml/slim) 模板。我希望我的生成器连接到现有的模板引擎并传递一些参数。
我的view_generator.rb 文件如下所示:
class ViewGenerator < Rails::Generators::NamedBase
source_root File.expand_path('../templates', __FILE__)
argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
def some_custom_method
(...)
end
hook_for :template_engine, :as => :scaffold
end
一切都像这样正常工作。我想在我的some_custom_method 中添加几个属性:
def some_custom_method
new_attribute = Rails::Generators::GeneratedAttribute.new("description")
new_attribute.type = :integer
attributes << new_attribute
end
发生的情况是我在attributes 数组中插入new_attribute,但是当hook_for 执行时,attribute 变量恢复为从命令行传递的原始变量。
我怎样才能绕过这个?
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.2 thor