开发者

Easy way to rename rails controllers [duplicate]

This question already has answers here开发者_Python百科: How to rename rails controller and model in a project (8 answers) Closed 7 years ago.

Is there an easy way to rename a controller? The only way I know of is to either do it by hand or generate a new controller move the code over and destroy the old one. Seems like there has to be a programmatic way to do this.


Some IDE's (like IntelliJ's RubyMine) will let you Refactor -> Rename a file/variable/method etc, although it's not as reliable in a dynamic language like Ruby as it is in a language like Java.


I had just generated a controller and so I did not have an associated model or database table. I decided to just rename all the files and relevant content that was created when I generated the controller. It is not an 'easy' way to rename the controller but I had confidence in my knowledge of what had been created and what I needed to refactor.

There is a good guide on the ruby on rails guides websites that shows what is generated and what you need to edit or you can see what a typical controller generates below:

$ bin/rails generate controller Greetings hello
 create  app/controllers/greetings_controller.rb
  route  get "greetings/hello"
 invoke  erb
 create    app/views/greetings
 create    app/views/greetings/hello.html.erb
 invoke  test_unit
 create    test/controllers/greetings_controller_test.rb
 invoke  helper
 create    app/helpers/greetings_helper.rb
 invoke  assets
 invoke    coffee
 create      app/assets/javascripts/greetings.js.coffee
 invoke    scss
 create      app/assets/stylesheets/greetings.css.scss

Also, don't forget to edit the contents of the files above, things like descriptions in your assets files, controller class names and module names etc.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜