8

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.

hadees
  • 1,754
  • 2
  • 25
  • 36

2 Answers2

5

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.

Zachary Wright
  • 23,480
  • 10
  • 42
  • 56
2

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.

atw
  • 5,428
  • 10
  • 39
  • 63