November 13, 2022
How to Undo Generate Scaffold Command in Rails
TL;DR: Run the bin/rails destroy scaffold Post to undo the changes made by scaffold command in Rails. If you had applied the generated migration, don't forget to rollback the migration using bin/rails db:rollback command first.
The rails generate scaffold command in Rails provides a quick and easy way to generate the major building pieces for your Rails app. It will generate the MVC (model, view, and controller) parts for your domain along with CRUD support. All in a single command.
For example, if you want to generate the basic CRUD functionality for the Post model in your Rails blog, run the following scaffold command.
$ bin/rails generate scaffold Post name:string content:text
Rails will create a bunch of new folders and files in your application, and also edit the config/routes.rb.
What if you made a mistake? Don't worry. Rails provides a simple way to undo the scaffold.
How to Undo Rails Scaffold Command
Simply run the following command:
$ bin/rails destroy scaffold Post
This will delete the files created by the scaffolding process.
If you had applied the migration generated by the scaffold, first rollback it using the db:rollback command, and then destroy the migration.
$ bin/rails db:rollback
$ bin/rails destroy scaffold Post
Also remember that the destroy scaffold command won't undo any other changes you made after generating the scaffold. So keep in mind to undo them all if needed.
In fact, you can use the destroy command to undo any generate command in Rails. For example, if you generated a helper using
$ bin/rails generate helper Payment
You can undo it using this command:
$ bin/rails destroy helper Payment
Hope that helps.
Subscribe to get future posts via email (or grab the RSS feed)