Rails 引擎中的自定义视图脚手架 [英] Custom Views Scaffolding in Rails Engines
问题描述
我正在尝试从我的引擎中获取自定义脚手架.
I'm trying to get custom scaffolding working from my engine.
我遵循了一些关于在普通 Rails 应用程序中自定义 Rails 3.2 脚手架的教程,并将我的自定义模板放在引擎 /lib/templates/erb/scaffold 目录中,但它们没有被包含引擎的应用程序.有什么建议吗?
I followed some tutorial on customizing Rails 3.2 scaffolding in a normal Rails App and put my customized templates in the engines /lib/templates/erb/scaffold directory but they don't get picked up by the app that includes the engine. Any suggestions?
更新:我还尝试覆盖 Rails ScaffoldGenerator 的 source_path
并尝试其他一些路径来放入我的模板,例如:lib/rails/generators/erb/scaffold/templates
Update:
I also tried to override the Rails ScaffoldGenerator's source_path
and tried some other paths to put my template in, like:
lib/rails/generators/erb/scaffold/templates
推荐答案
zarazan 的回答让我大致了解了这一点,但它有一些问题.以下是对我有用的方法:
zarazan's answer got me most of the way there, but there are a couple of things wrong with it. Here's what worked for me:
class Engine < Rails::Engine
config.generators do |g|
g.templates.unshift File::expand_path('../../templates', __FILE__)
end
end
请注意,这是在 generators
部分,不是 app_generators
,并且路径略有不同.
Note that this goes in the generators
section, not app_generators
, and that the path is slightly different.
另外,我认为存储模板的正确路径是 lib/templates/erb/scaffold
,可以选择用您使用的任何语言(如 haml 或 slim)替换 erb.我知道这有效为苗条.文件名是 {_form,edit,index,new,show}.html.erb
.
Also, I think the correct path to store your templates is lib/templates/erb/scaffold
, optionally replacing erb with whatever language you are using (like haml or slim.) I know this works for slim. The file names are {_form,edit,index,new,show}.html.erb
.
这篇关于Rails 引擎中的自定义视图脚手架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!