1

i have a lot of scaffold and all of them are similar in views and controller. my problem is that Every time I generate a new scaffold had to change views and controller, Repeated changes :(

Can I generate a new scaffold with new controller and views that I want?

Zakaria
  • 983
  • 15
  • 24

3 Answers3

2

In Rails you can customize the default layouts used by the generator. There are several tutorials available online.

You can change the model file, the controller and/or the action templates. As explained in this answer

You can override default view templates by creating your own templates in lib/templates/erb/scaffold folder of your rails app.

lib/templates/erb/scaffold/_form.html.erb
lib/templates/erb/scaffold/edit.html.erb
lib/templates/erb/scaffold/index.html.erb
lib/templates/erb/scaffold/new.html.erb
lib/templates/erb/scaffold/show.html.erb  
Community
  • 1
  • 1
Simone Carletti
  • 173,507
  • 49
  • 363
  • 364
0

You can use layout to solve this problem. Create a html.erb view file under views/layouts folder and set layouts accordingly using layout :layout_file_name in your controller

sansarp
  • 1,446
  • 11
  • 18
  • no, i generate a scaffold and i have index, show, new, _form and edit all .heml.erb, and i had to change this views,i want customize this views . – Zakaria Feb 14 '15 at 18:11
  • If you have some portions of your view say Header and footer portion common then in that case you could use partial files and include those partial files wherever necessary. If it is not the case and if your are not using layouts then you have to customize each view separately. Hope this clears your confusion. – sansarp Feb 14 '15 at 18:17
0

If you're looking for a lightweight alternative to the scaffold generator, you can just generate a resource. This will give you a model, controller, and a view folder without the standard scaffold code. This will give you a blank set of views and a blank controller to work with.

$ rails generate resource MODEL attributes 
JeffD23
  • 8,318
  • 2
  • 32
  • 41