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条回答
对你真心纯属浪费
2楼-- · 2019-01-09 21:18

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.

查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-01-09 21:18

you can revert your rails g/generate controller/model/migration xxx output by using:

 rails d/destroy controller/model/migration xxx
查看更多
可以哭但决不认输i
4楼-- · 2019-01-09 21:18

To reverse that, we just destroy it. Open Terminal application and go to 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 model,migration and some of related test files. (You can see result on Terminal after you have run the command.)

查看更多
一纸荒年 Trace。
5楼-- · 2019-01-09 21:18

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 ____`

查看更多
放我归山
6楼-- · 2019-01-09 21:20

This is 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 User Controller

rails generate controller User

or

rails g controller User

if want destroy User controller or revert to above action then

rails destroy controller User

or

rails d controller User

enter image description here

查看更多
贪生不怕死
7楼-- · 2019-01-09 21:20

to reverse rails generate, use rails destroy

rails destroy Model

https://guides.rubyonrails.org/command_line.html#rails-destroy

查看更多
登录 后发表回答