How to reverse a 'rails generate'
Asked Answered
H

17

519

I want to delete all the files it created and roll back any changes made, but not necessarily to the database, but more to the config files.

I'd like to automatically delete all the resource mappings for the model/controller deleted in the routes.rb file and everywhere else that changes might have been made?

Hostile answered 12/11, 2010 at 3:23 Comment(0)
M
1003
rails destroy controller lalala
rails destroy model yadayada
rails destroy scaffold hohoho

Rails 3.2 adds a new d shortcut to the command, so now you can write:

rails d controller lalala
rails d model yadayada
rails d scaffold hohoho
Matzo answered 12/11, 2010 at 3:29 Comment(11)
Btw, how do I use this command to delete a scaffold? The model and controller work fine...but how do I completely reverse a scaffold?Hostile
Ok, I figured it out. Perhaps I was mistyping something. I just ran rails destroy scaffold lalalal and that worked.Hostile
@Hostile Must've been that extra 'L'.Vulgar
What about removing files associated with a gem install such as rails generate <gemname>:installOus
@doug, I don't think that's possible. Your best bet is to just rerun the generator with the -f flag to force it to recreate/reedit the files… Then you can see which files it created/changed, and proceed on manually deleting them.Blockbuster
For destroying controller write plural name (rails d controller comments)Nucleo
Wow I wasn't expecting this functionality! Thought you had to use Git commits throughoutSyncope
Why do the steps not include db rollback as well and schema.rb update?Flawy
You yadayada'd over the best part.Pulcheria
Note that you will have to rollback any migrations created with scaffold before deleting the scaffold itself.Blunder
To destoy generated gem install need to type -> rails destroy <gemname>:installBracken
C
59

It's worth mentioning the -p flag here ("p" for pretend).

If you add this to the command it will simply do a "test" run and show you what files will be deleted without actually deleting them.

$ rails d controller welcome -p

  remove  app/controllers/welcome_controller.rb
  invoke  erb
  remove    app/views/welcome
  invoke  test_unit
  remove    test/controllers/welcome_controller_test.rb
  invoke  helper
  remove    app/helpers/welcome_helper.rb
  invoke    test_unit
  remove      test/helpers/welcome_helper_test.rb
  invoke  assets
  invoke    coffee
  remove      app/assets/javascripts/welcome.js.coffee
  invoke    scss
  remove      app/assets/stylesheets/welcome.css.scss

If you're happy with it, run the command again without the -p flag.

Checker answered 12/12, 2014 at 9:44 Comment(1)
I think this is the best answerDeform
B
26

rails destroy controller Controller_name was returning a bunch of errors. To be able to destroy controller I had to remove related routes in routes.rb. P.S. I'm using rails 3.1

Bazluke answered 18/11, 2011 at 15:59 Comment(1)
Please don't summarize your actions unless you intend for this answer to be the selected correct answer.Implicatory
B
13

This is a prototype to generate or destroy a controller or model in Rails:

rails generate/destroy controller/model [controller/model Name]

For example, if you need to generate a User Controller:

rails generate controller User

or

rails g controller User

If you want to destroy the User controller or revert to above action then use:

rails destroy controller User

or:

rails d controller User

enter image description here

Bashuk answered 14/2, 2014 at 10:11 Comment(0)
C
7

You could use rails d model/controller/migration ... to destroy or remove the changes generated by using the rails generate command.

For example:

rails g model Home name:string

creates a model named home with attribute name. To remove the files and code generated from that command we can use

rails d model Home
Cineaste answered 7/6, 2013 at 14:57 Comment(0)
B
5

If you use Rails, use rails d controller Users.

And, if you use Zeus, use zeus d controller Users.

On the other hand, if you are using git or SVN, revert your changes with the commit number. This is much faster.

Behemoth answered 28/7, 2014 at 10:25 Comment(0)
I
4

Before reverting the rails generate, please make sure you rollback the migration first.

Case 1: if you want to revert scaffold then run this command:

rails destroy scaffold MODEL_NAME

Case 2: if you want to revert model then run this command:

rails destroy model MODEL_NAME

Case 3: if you want to revert controller then run this command:

rails destroy controller CONTROLLER_NAME

Note: you can also use shortcut d instead of destroy.

Insipience answered 11/2, 2017 at 6:45 Comment(0)
P
3

You can revert your

rails g/generate controller/model/migration xxx

output by using:

 rails d/destroy controller/model/migration xxx
Pregnancy answered 19/3, 2014 at 14:15 Comment(0)
E
3

If you prefer to delete the controller manually:

For controller welcome

rm app/controllers/welcome_controller.rb
rm app/views/welcome
rm test/controllers/welcome_controller_test.rb
rm app/helpers/welcome_helper.rb
rm test/helpers/welcome_helper_test.rb
rm app/assets/javascripts/welcome.js.coffee
rm app/assets/stylesheets/welcome.css.scss
Entire answered 1/10, 2015 at 19:40 Comment(0)
M
3

You can undo a rails generate in the following ways:

  • For the model: rails destroy MODEL
  • For the controller: rails destroy controller_name
Menes answered 5/11, 2015 at 9:28 Comment(0)
V
3

Suppose I have created a controller named "sample" like:

rails generate controller sample

If I have to destroy this controller, all I have to do is swap generate with destroy, as in

rails destroy controller sample.

If you want to reverse the generation, all you have to do is swap generate with destroy.

Vezza answered 22/12, 2017 at 5:10 Comment(0)
P
3

To reverse rails generate, use rails destroy:

rails destroy Model

See "rails destroy" for more information.

Parks answered 25/10, 2018 at 12:30 Comment(0)
P
3

You can destroy all things that was created same way except little thing change. For controller,

rails d controller_name (d stands for destroy)

For Model

rails d model_name

you just put d(destroy) instead of g(generate) in your migration.

Pertain answered 4/1, 2019 at 6:21 Comment(0)
D
2

All versions of rails have a "destroy", so-, if you create a (for example) scaffold named "tasks" using a generator, to destroy all the changes of that generate step you will have to type:

rails destroy scaffold Tasks

Hope it helps you.

Dichotomy answered 5/8, 2016 at 12:52 Comment(0)
I
1

To reverse that, we just destroy it. Open the Terminal application and go to the project directory, then, type this:

rails destroy model CamelCase
rails destroy controller CamelCase

Where CamelCase is a name of any model or controller. It will remove the model, migration and some of the related test files. (You can see the result in the Terminal window after you have run the command.)

Imaginary answered 2/8, 2015 at 1:55 Comment(0)
A
0

Removed scaffolding for selected model:

bin/rails d scaffold <AccessControl> //model name
Ariellearies answered 22/5, 2017 at 5:36 Comment(0)
V
0

We use generate as rails generate app. So regenerating any generate statement can be reversed using destroy statement. Just replace generate with destroy i.e. rails generate app can be written as rails destroy app' rails generate ____asrails destroy ____`

Velocity answered 30/1, 2018 at 4:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.