How do I reverse a 'rails generate'?

2019-01-09 20:47发布

i.e. delete all the files it created and roll back any changes made? Not necessarily to the db, but more to the config files.

E.g. automatically deleting all the resource mappings for the model/controller deleted in the routes.rb file and everywhere else that changes might have been made?

Thanks.

18条回答
Evening l夕情丶
2楼-- · 2019-01-09 21:07

Suppose I have created a controller named sample like as

rails generate controller sample

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

rails destroy controller sample.

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

查看更多
一夜七次
3楼-- · 2019-01-09 21:07

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

查看更多
迷人小祖宗
4楼-- · 2019-01-09 21:10

Are you using version control (subversion, git, whatever)? Just revert. If not - why not?!!

查看更多
狗以群分
5楼-- · 2019-01-09 21:10

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

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 the command rails d model Home.

查看更多
Fickle 薄情
6楼-- · 2019-01-09 21:10

To delete the controller manually, in case the rails d controller command does not work:

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
查看更多
小情绪 Triste *
7楼-- · 2019-01-09 21:17

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.

查看更多
登录 后发表回答