How do I reverse a 'rails generate'?

老子叫甜甜 提交于 2019-11-26 14:57:29

问题


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.


回答1:


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



回答2:


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:


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




回答4:


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




回答5:


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




回答6:


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.




回答7:


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

 rails d/destroy controller/model/migration xxx



回答8:


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'




回答9:


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



回答10:


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.




回答11:


You can undo a rails generate in following ways:

  • For Model: rails destroy MODEL
  • For Controller : rails destroy controller_name



回答12:


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.




回答13:


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.)




回答14:


All generations of rails have a destroy of them, so , if you create by generator a (for example) scaffold named "tasks", to destroy all the changes of that ganerate you will have to type:

rails destroy scaffold Tasks

Hope it helps you.




回答15:


Removed scaffolding for selected model

bin/rails d scaffold <AccessControl> //model name



回答16:


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




回答17:


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




回答18:


to reverse rails generate, use rails destroy

rails destroy Model

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



来源:https://stackoverflow.com/questions/4161357/how-do-i-reverse-a-rails-generate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!