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.
-
I'm not so clear on what the difficulty is... Are you worried about renaming routes and links? – Andres Jaan Tack Aug 03 '10 at 00:34
-
It is just a lot of work when you have a bunch, I'm looking at moving a lot of my controllers into a namespace for better organization. – hadees Aug 03 '10 at 04:00
-
1@Andres: a difficult part is moving the database too. RubyMine forgets it when refactoring. – Nicolas Raoul Jul 04 '11 at 03:38
-
Manually renaming is the option you have :) IDE's may not help that much – Pramod Solanky Sep 19 '14 at 07:03
-
This question is similar to this [post][1]. [1]: http://stackoverflow.com/questions/11924124/how-to-rename-rails-controller-and-model-in-a-project – sylvee Aug 09 '15 at 20:56
2 Answers
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.

- 23,480
- 10
- 42
- 56
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.

- 5,428
- 10
- 39
- 63