ruby on rails - tutorial - Undo scaffolding in Rails
ruby on rails scaffold tutorial (16)
Is there any way to 'undo' the effects of a scaffold command in Rails?
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
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.
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 :)
Best way is :
destroy rake db: rake db:rollback
For Scaffold:
rails destroy scaffold Name_of_script
For generating scaffold in rails -
rails generate scaffold MODEL_GOES_HERE
For undo scaffold in rails -
rails destroy scaffold MODEL_GOES_HERE
If you just want to see the files a generator will create you can pass the generator --pretend or -p option.
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.
Rishav Rastogi is right, and with rails 3.0 or higher its:
rails generate scaffold ...
rails destroy scaffold ...
To generate scaffolding :
rails generate scaffold xyz
To revert scaffolding :
rails destroy scaffold xyz
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
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
You can undo whatever you did with
rails generate xxx
By
rails destroy xxx
For example this applies generators to migration, scaffold, model...etc
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
use this
rails d scaffold MODEL_NAME
rake db:rollback
you need to rollback the migrations first by doing rake db:rollback if any And then destroy the scaffold by
rails d scaffold foo
rails d scaffold <scaffoldname>
Also, make sure you undo the migration you made either by rollback or to a particular version.