Is there any way to 'undo' the effects of a scaffold command in Rails?
24 Answers
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.
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
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.
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.