11
votes

The scaffold generator in rails generates the MVC layers from the templates located in lib/rails/generators/erb/scaffold/templates railties path and from the lib/templates folder in your project.

If you copy the original files from the railties folder to your projects lib folder, then you'll be able to customize the original scaffold generator, as Daniel Fone explain in his blog.

The original Rails generator uses 5 files in the views layer:

  • _form.html.erb
  • edit.html.erb
  • index.html.erb
  • new.html.erb
  • show.html.erb

I was wondering if the there is a way to extend this behavior by adding another file to the set. like _header.html.erb or _info.html.erb (some partials that I've design).

2

2 Answers

20
votes

Well after a couple months I've found the solution. Instead of creating a new generator as the Rails documentation states, I override the default generator but in my projects lib folder.

enter image description here

The original scaffold_generator.rb is located at ~/.rvm/gems/ruby-2.1.0/gems/railties-4.2.4/lib/rails/generators/erb/scaffold. To add the new file (_info.html.erb) we will add it to the available_views method.

def available_views
    %w(index edit show new _form _info)
end

Hope it helps someone.

7
votes

For those who are lazy, here is a quick command to copy the default railties erb templates to the correct location in Rails:

mkdir -p lib/templates/erb/scaffold && \
cp $(bundle info railties --path)/lib/rails/generators/erb/scaffold/templates/* lib/templates/erb/scaffold