14

I'm using Rails 3.2, and have started with a scaffolding and built out from it, but have realized I need to rename the entire scaffold (Model, View, Controller, db:migrate, etc). Is there a built in way to do this, or should I just do it manually?

azz
  • 5,852
  • 3
  • 30
  • 58

3 Answers3

10

I don't think there's anything rails provides to rename the name of models/controllers/views/tests etc. once they are created - whether as a part of a scaffold, or not.

You will have to change it manually.

If it is a brand new app that you have just started on, it might be easier to just delete the whole directory/drop the database, and start over again.

If not, you will have to go through the files created/modified by the scaffold generation, and modify them manually.

Make sure you either drop_and_recreate the relevant table, or add a migration to rename the table. See How do you write a migration to rename an ActiveRecord model and its table in Rails? for some relevant advice.

Community
  • 1
  • 1
Prakash Murthy
  • 12,923
  • 3
  • 46
  • 74
  • Ok, I did that and migrated the db, now I have 2 (linked) problems. `No route matches [GET] "/new_name"` (plural) and `undefined local variable or method `new_name_path'` (plural). I figure I need to update my routing, but have no idea how. – azz Nov 02 '12 at 03:44
  • 1
    In the `./config/routes.rb` file, the entry `resources :` should be changed to `resources :` – Prakash Murthy Nov 02 '12 at 04:21
  • Yeah I just did that and things are working. The old_name wasn't in the routes file though, which was confusing. All sorted I think. Thanks. – azz Nov 02 '12 at 04:34
1

I think there's no out of the box method to rename files generated by scaffold.What you have to do is create a new scaffold and copy your codes from the old to the new.Copy contents from factories, model, controller and their respective spec file to the new scaffold. Then remove the old scaffold with the command

rspec d scaffold <Model Name>

and you have to create a new migration to drop that old table from the database. Then run your migrations.

Nathaniel
  • 71
  • 1
  • 6
0

My app wasn't in production yet so I could afford to drop the db and re create it.

I was also fortunate in that the singular of the old and new terms both end in 's' (e.g. 'apples' 'oranges').

Here's what I did:

  1. Use sublime text (it's the IDE with the best search IMO) to case sensitive find and replace the whole <current folder> for the term first with upper case then with lower case; two separate searches.

  2. Browse the searches and create a list of files you don't want to change (we'll ensure they haven't changed at the end).

  3. Do the find and replace on upper case and with lower case (do the find and replace on the singular of the word, not the plural - e.g. 'session', not 'sessions')

  4. Now use git checkout filename on the files you didn't want to alter (so they go back to being as they were).

  5. Personally, I made a commit here in case I messed anything up in the next step

  6. Now change directory and file names. Identify files/directories that need changing with find . -type f -iname "*oldterm*" (that's for mac, you may have to look up how if on another OS).

  7. Change the directories/file names found in the results above.

  8. Bonus: to keep git happy, try using git mv on both files and directories so your commit history is much cleaner.

stevec
  • 41,291
  • 27
  • 223
  • 311