Custom Views Scaffolding in Rails Engines

烂漫一生 提交于 2019-12-10 17:44:06

问题


I'm trying to get custom scaffolding working from my engine.

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?

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


回答1:


In the file that you declare your engine use this command:

class Engine < Rails::Engine

  config.app_generators do |g|
    g.templates.unshift File::expand_path('../templates', __FILE__)
  end

end

It should shift the preference of what template folder Rails uses by default.

Now just put the template files in lib/templates/erb/scaffold/template_name.erb

Where template_name is one of the following: _form.html.erb, edit.html.erb, index.html.erb, new.html.erb, show.html.erb

Once you include the gem you should be able to use the rails generate scaffold command as normal.

Here is an example of an engine that overrides the default scaffolding in rails:

https://github.com/brocktoncg/gemboree

This is where the template directory is located:

https://github.com/brocktoncg/gemboree/tree/master/lib/templates/erb/scaffold




回答2:


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

Note that this goes in the generators section, not app_generators, and that the path is slightly different.

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.




回答3:


Are you talking about a controller template? Than you are using the wrong directory. Save your template at lib/templates/rails/scaffold_controller/controller.rb

Have a look at http://xyzpub.com/en/ruby-on-rails/3.2/templates.html for an example.



来源:https://stackoverflow.com/questions/16420820/custom-views-scaffolding-in-rails-engines

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!