How to Reverse a 'Rails Generate'

How to reverse a 'rails generate'

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

How to reverse rails generate gemname :install?

rails destroy <gemname>:install

Usually works.

Undo scaffolding in Rails

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.

rails destroy controller does nothing

Try to remove routes manually and retry deleting with

rails d controller welcome -p

-p will post output to terminal. Check it for errors. If there are any errors, update answer with information about them. And if no errors are shown, try command again without -p.

Here is a post with lots of comments and answers about this command.

How to rollback a specific migration?

rake db:rollback STEP=1

Is a way to do this, if the migration you want to rollback is the last one applied. You can substitute 1 for however many migrations you want to go back.

For example:

rake db:rollback STEP=5

Will also rollback all the migration that happened later (4, 3, 2 and also 1).

To roll back all migrations back to (and including) a target migration, use: (This corrected command was added AFTER all the comments pointing out the error in the original post)

rake db:migrate VERSION=20100905201547

In order to rollback ONLY ONE specific migration (OUT OF ORDER) use:

rake db:migrate:down VERSION=20100905201547

Note that this will NOT rollback any interceding migrations -- only the one listed. If that is not what you intended, you can safely run rake db:migrate and it will re-run only that one, skipping any others that were not previously rolled back.

And if you ever want to migrate a single migration out of order, there is also its inverse db:migrate:up:

rake db:migrate:up VERSION=20100905201547

how do I reverse rails g controller some_namespace::some_controller?

rails destroy controller some_namespace::some_controller

I just ran it on my system, here is the output:

 rails destroy controller my::items
remove app/controllers/my/items_controller.rb
remove app/views/my/items
invoke helper
remove app/helpers/my/items_helper.rb

How to Undo a rails destroy controller?

Once controller is destroyed using rails destroy controller ... command, it can't be undone.

Be sure that you've version controlled your code using git or similar VCS tools. If your code is on git then you can easily get all deleted files back.

git checkout app/controllers/pages_controller.rb


Related Topics



Leave a reply



Submit