508

Is there any way to 'undo' the effects of a scaffold command in Rails?

Daniel
  • 16,026
  • 18
  • 65
  • 89

25 Answers25

813

First, if you have already run the migrations generated by the scaffold command, you have to perform a rollback first.

rake db:rollback

You can create scaffolding using:

rails generate scaffold MyFoo 

(or similar), and you can destroy/undo it using

rails destroy scaffold MyFoo

That will delete all the files created by generate, but not any additional changes you may have made manually.

Rishav Rastogi
  • 15,484
  • 3
  • 42
  • 47
  • 12
    dont forget to `rake db:rollback` before you destroy your scaffold. This will destroy/drop the table if there is no other migration except the scaffold's migration before it. – Yakob Ubaidi May 08 '14 at 11:06
  • 3
    In some cases there are still leftover files that were not deleted from the scaffold. Check your version control, e.g. `git status` and/or `git diff`, to see if the destroy command missed anything. – aaron-coding Mar 31 '15 at 21:58
  • 1
    If you ran rake db:migrate did some other migrations after that then it makes sense to create a migration to destroy the table before "destroying" the scaffold. so do something like rails g migration DropMyFoos and then open the migration file and add code for dropping the table. Run this migration and then run the destroy scaffold. – Vikram Sharma Jan 04 '16 at 13:46
  • Also keep in mind that it will remove the view folder for my_foo, even if you have extra files there. – Andrew K Sep 02 '17 at 21:22
168

Rishav Rastogi is right, and with rails 3.0 or higher its:

rails generate scaffold ...
rails destroy scaffold ...
Travis Pessetto
  • 3,260
  • 4
  • 27
  • 55
Misha Rabinovich
  • 1,689
  • 1
  • 10
  • 2
  • 8
    @femi The table was made with rake. The best way to remove it is running `rake db:rollback` before `rails destroy scaffold` or if it is not the last one you did rake `db:migrate:down VERSION=20080906120000` you can find the number before its name in db/migrate – Travis Pessetto Aug 04 '11 at 18:22
40

You can undo whatever you did with

rails generate xxx

By

rails destroy xxx

For example this applies generators to migration, scaffold, model...etc

thekindofme
  • 3,846
  • 26
  • 29
13

If you just want to see the files a generator will create you can pass the generator --pretend or -p option.

Sudhanshu Arya
  • 149
  • 1
  • 6
10

Best way is :

destroy rake db:  rake db:rollback

For Scaffold:

rails destroy scaffold Name_of_script
Kick Buttowski
  • 6,709
  • 13
  • 37
  • 58
user917158
  • 443
  • 1
  • 6
  • 11
9

for first time, you can check you database migration if you have generate scaffold. you must destroy them to clean up your database

rake db:rollback

then

rails d scaffold
Aamir
  • 16,329
  • 10
  • 59
  • 65
dedennufan
  • 91
  • 1
  • 2
8
rails d scaffold <scaffoldname>

Also, make sure you undo the migration you made either by rollback or to a particular version.

RageCore
  • 281
  • 2
  • 3
4

To generate scaffolding :

rails generate scaffold xyz

To revert scaffolding :

rails destroy scaffold xyz
Arslan Ali
  • 17,418
  • 8
  • 58
  • 76
uma
  • 2,932
  • 26
  • 20
4

Rails destroy name

rake db:rollback
Aamir
  • 16,329
  • 10
  • 59
  • 65
starbuck
  • 171
  • 3
  • 12
3

For generating scaffold in rails -

rails generate scaffold MODEL_GOES_HERE

For undo scaffold in rails -

rails destroy scaffold MODEL_GOES_HERE
Amit Suroliya
  • 1,515
  • 1
  • 11
  • 21
2

you need to roll back migrations too after destroying scaffold too

rails destroy scaffold 'scaffoldname'
rake db:rollback
Bhargav Mehta
  • 389
  • 7
  • 18
2

So, Process you should follow to undo scaffolding in rails 4. Run Command as below:

  1. rails d scaffold FooBar
  2. rake db:rollback if you_had_run_rake db:migrate after creating above scaffold?

That's it!

Cheers!

Manish Shrivastava
  • 30,617
  • 13
  • 97
  • 101
2

use this

rails d scaffold MODEL_NAME

rake db:rollback
vipin
  • 2,374
  • 1
  • 19
  • 36
2

Recommend rollback First ,type in your Terminal.

rake db:rollback

Add destroy scaffold (the 'd' stands for 'destroy')

rails d scaffold name_of_scaffold

Enjoy your code.

Chutipong Roobklom
  • 2,953
  • 2
  • 17
  • 20
1

you need to rollback the migrations first by doing rake db:rollback if any And then destroy the scaffold by

rails d scaffold foo
Aamir
  • 16,329
  • 10
  • 59
  • 65
Manish
  • 263
  • 2
  • 12
1

First you will have to do the rake db:rollback for destroy the table
if you have already run rake db:migrate and then you can run

rails d scaffold Model

Shoaib Malik
  • 373
  • 4
  • 8
1

Yes, the scaffold itself and all the things that amalgamate it.

The destroy command is the opposite of generate and will undo one. Just pass it the name the same way did with generate and it'll be scrubbed from your project:

rails generate scaffold posts title:string content:text
rails destroy scaffold posts title:string content:text
JackHasaKeyboard
  • 1,599
  • 1
  • 16
  • 29
1

To generate the scaffold:

rails generate scaffold abc

To revert this scaffold:

rails destroy scaffold abc

If you have run the migration for it just rollback

rake db:rollback STEP=1
nitanshu verma
  • 261
  • 3
  • 8
1

provider another solution based on git

start a new project

rails new project_name
cd project_name

initialize git

git init
git commit -m "initial commit"

create a scaffold

rails g scaffold MyScaffold
rake db:migrate

rollback the scaffold

rake db:rollback
git reset --hard
git clean -f -d
Ray Lee
  • 111
  • 1
  • 2
1

rails [option] scaffold scaffold_name

Option

g    generate
d    destroy

If you do

rails g  scaffold myFoo

Then reverse it back using

rails d scaffold MyFoo
krishnar
  • 2,537
  • 9
  • 23
1

To generate:

rails g scaffold post
rake db:migrate

To delete:

rake db:rollback
rails d scaffold post
0

When we generate scaffold, following files will be created:

Command: rails generate scaffold Game

Files created:

>       invoke  active_record
>       create    db/migrate/20160905064128_create_games.rb
>       create    app/models/game.rb
>       invoke    test_unit
>       create      test/models/game_test.rb
>       create      test/fixtures/games.yml
>       invoke  resource_route
>        route    resources :games
>       invoke  inherited_resources_controller
>       create    app/controllers/games_controller.rb
>       invoke    erb
>       create      app/views/games
>       create      app/views/games/index.html.erb
>       create      app/views/games/edit.html.erb
>       create      app/views/games/show.html.erb
>       create      app/views/games/new.html.erb
>       create      app/views/games/_form.html.erb
>       invoke    test_unit
>       create      test/controllers/games_controller_test.rb
>       invoke    helper
>       create      app/helpers/games_helper.rb
>       invoke      test_unit
>       create        test/helpers/games_helper_test.rb
>       invoke    jbuilder
>       create      app/views/games/index.json.jbuilder
>       create      app/views/games/show.json.jbuilder
>       invoke  assets
>       invoke    coffee
>       create      app/assets/javascripts/games.js.coffee
>       invoke    scss
>       create      app/assets/stylesheets/games.css.scss
>       invoke  scss
>       create    app/assets/stylesheets/scaffolds.css.scss

If we have run the migration after this then we have to rollback the migration first as the deletion of scaffold will remove the migration file too and we will not able to revert that migration.

Incase we have run the migration:

rake db:rollback

and after this we can safely remove the scaffold by this commad.

rails d scaffold Game

This command will remove all the files created by the scaffold in your project.

BIlal Khan
  • 453
  • 3
  • 16
0

Any time you run rails g, you can reverse it by running rails d (destroy) to remove what you've generated. If you have already run rake db:migrate, you will need to run rake db:rollback before destroying :)

Maddie
  • 83
  • 2
  • 7
0

Case 1: If you run only this command to generate scaffold -

rails generate scaffold MODEL_NAME FIELD_NAME:DATATYPE

Ex - rails generate scaffold User name:string address:text

but till now you did not run any command for migration like

rake db:migrate

then you should need to run only this command like -

rails destroy scaffold User name:string address:text

Case 2: If you already run(Scaffold and Migration) by below commands like -

rails generate scaffold User name:string address:text

rake db:migrate 

Then you should need to run first rollback migration command then destroy scaffold like below -

rake db:rollback

rails destroy scaffold User name:string address:text

So In this manner, we can undo scaffolding. Also we can use d for destroy and g for generate as a shortcut.

Manoj Kumar
  • 316
  • 1
  • 7
0
rails g scaffold MyFoo 

for generating and

rails d scaffold MyFoo

for removing

Ayaz Ahmad Tarar
  • 534
  • 8
  • 24