I started a Rails app and everything works fine. But now, I would like to rename a controller and the associated model:
I wanted to change the Corps
controller to Stores
and the same (without final s) for the model.
Looking on google, people suggested to destroy and then generate again the controller and model. The problem is that it will erase the actual code of each files!
Any solution? Thanks in advance.
As someone that just finish this painful process the MOST important step is to build enough TESTS to check as much functionality as possible. They should cover not only the model/controller that you plan to rename but also all other models/controllers/views parts. Anyhow it's a good (or maybe even a must) practice.
Do this steps by iterations, sometimes you need to comeback to steps few times (5 and more) to discover additional files that need to be changed. And now for the rename steps:
Corps/Corp
toStores/Store
indb/migrate
folderTry to run:
rake db:drop:all
rake db:create
rake db:migrate
Change content of
db/seeds.rb
file.rake db:seed --trace
(In this step you may need to change some other model/controller files.)test/fixtures
files. you may need to change not only corps.yml but other related files (some files may include corp_id).TESTOPTS="--seed=1981"
or any other number)One other important thing is that you need to update the model associations, which you'll have to do whether you rename manually or destroy and generate the resource (since they exist in the other models). You can either run a migration to change the column names of the foreign keys in the database and change all references to those foreign keys in the code:
or set a custom foreign key when declaring the association that uses the old foreign key:
Also if your resource includes images, they are often stored in a directory that includes the resource name but (with carrierwave at least) once the resource's name is changed they'll be referenced incorrectly (file exists at '/uploads/old/image/1/pic.jpg' but is looked for at 'uploads/new/...'), so you'll have to either delete and re-upload the images, move them to the new path, or perhaps change where they're being looked for.
And if you have model tests, you need to change:
File rename: corp_test.rb -> store_test.rb (also for controller tests, integration tests, fixture, etc.)
Code of store_test.rb: Change class CorpTest for class StoreTest.
And all the references of corp in the controller, model, integration, fixture tests.
In addition to Nobita answer you similarly need to change the test & helper class definitions & file names for
corps
tostore
. More Importantly you should changecorps
tostore
in your config/routes.rb fileSo in total you're making changes to the Controller, associated Model, Views, Helpers, Tests and Routes files.
I think what you’ve seen suggested with
destroy
&generate
is a better option. I’ve given an answer how to do this here: Rails : renaming a controlller and corresponding modelfor controller you will have to make change in following places if you're doing it manually:
for model, Nobita's answer is pretty good
You can try the Rails Refactor gem too, a Command line tool for simple refactors like rename model and controller for Rails projects
Usage: