Victor
Victor

Reputation: 13368

Change Rails controller name

Let's say I have a controller and model called Car/Cars in Rails. Then I wanna change it to Vehicle/Vehicles. How to I achieve that?

Upvotes: 1

Views: 3455

Answers (4)

Nikhil Thombare
Nikhil Thombare

Reputation: 1198

To install: gem install rails_refactor

Before use, make sure you cd to the root of your rails project.

To rename a controller:

rails_refactor rename OldController NewController

renames controller file & class name in file
renames controller spec file & class name in file
renames view directory
renames helper file & module name in file
updates routes

To rename a controller action:

$ rails_refactor rename DummyController.old_action new_action

renames controller action in controller class file
renames view files for all formats

To rename a model:

$ rails_refactor rename OldModel NewModel

renames model file & class name in file
renames spec file & class name in file
renames migration & class name & table names in file```

Upvotes: 0

Igor
Igor

Reputation: 2909

Things to take care of when changing controller name

  • Controller name and optionally directory name
  • Controller class name in class XxxController < ApplicationController
  • Associated routes in routes.rb
  • *_path and *_url URL helper instances

Upvotes: 0

theIV
theIV

Reputation: 25774

I don't know if there's any sort of automated process in Rails to do this. It boils down to a lot of find/replace for your instances of Car with Vehicle and cars with vehicles, as well as in all filenames, routes, and the database.

Upvotes: 2

shingara
shingara

Reputation: 46914

You need change the name of file, the name of director and in last, the name of Class.

Upvotes: 3

Related Questions