Ruby on rails 在传递到rails生成器中的挂钩之前更改参数
我正在尝试制作简单的视图生成器,并使用DRY原理,我不想拥有自己的html(erb/haml/slim)模板。我希望我的生成器连接到现有的模板引擎,并向其传递一些参数 我的Ruby on rails 在传递到rails生成器中的挂钩之前更改参数,ruby-on-rails,ruby-on-rails-3,ruby-on-rails-3.2,thor,Ruby On Rails,Ruby On Rails 3,Ruby On Rails 3.2,Thor,我正在尝试制作简单的视图生成器,并使用DRY原理,我不想拥有自己的html(erb/haml/slim)模板。我希望我的生成器连接到现有的模板引擎,并向其传递一些参数 我的view\u generator.rb文件如下所示: class ViewGenerator < Rails::Generators::NamedBase source_root File.expand_path('../templates', __FILE__) argument :attributes, :t
view\u 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
class ViewGenerator < Rails::Generators::NamedBase
# your code ...
def self.start(args, config)
args.insert(1, 'description:integer') # 0 being the view name
super
end
end
发生的情况是,我在attributes
数组中插入new\u属性
,但是当执行hook\u for
时,attribute
变量恢复为从命令行传递的原始变量
如何绕过此问题?在调用某个自定义方法时,属性已经设置好(通过
ARGV
),通过检查代码,我看不到从那里更改属性的明确方法。您可以使用另一种方法,在生成器中重写start
class方法并直接操作args,如下所示:
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
class ViewGenerator < Rails::Generators::NamedBase
# your code ...
def self.start(args, config)
args.insert(1, 'description:integer') # 0 being the view name
super
end
end
classviewgenerator
谢谢你的回答!我插入了那个方法,但仍然不起作用。我试着把它公之于众,私之于众。另外,我不确定它是否会被调用,因为我插入了一些调试“puts”调用,但控制台上没有打印任何内容。我的错,我使用了错误的函数名称,请再次检查答案。