How to change a Ruby on Rails application name?

2019-01-08 05:14发布

问题:

I have a Ruby on Rails application that was created using:

rails new old_name -d mysql

Now I want to change the application name to be new_name.

Just changing the application folder name will not be enough, because the database name, for example, also has to be changed (from old_name_development to new_name_development). I wonder if there are other places in the automatically generated files that needs changing.

Is there any built in command for changing the application name ?

回答1:

There's a Rails plugin to do it for you. Super convenient.

https://github.com/get/rename



回答2:

In Rails 3, there are references to the application name in the following files:

  • config/application.rb
  • config/environment.rb
  • config/environments/development.rb
  • config/environments/production.rb
  • config/environments/test.rb
  • config/initializers/secret_token.rb
  • config/initializers/session_store.rb
  • config/mongoid.yml (if using Mongoid)
  • config/routes.rb
  • config.ru
  • Rakefile
  • app/views/layouts/application.html.erb, in title tag


回答3:

in Rails 3, the application class is defined in config/application.rb, and referred to in your environment files (config/environment.rb, config/environments/*.rb) and config/routes.rb. I think that's it, but you should find out pretty quickly from rails server if you missed one. :)

  • config/application.rb
  • config/enviroment.rb
  • config/environments/*.rb
  • config/routes.rb

That said, unless you've got a very specific reason for wanting it changed, I wouldn't bother. Doesn't really affect the application in any other way.



回答4:

Rails 3 application can rename using https://github.com/morshedalam/rename



回答5:

Run the following to replace the old_name for new_name from the root of your Rails (3.0.7) project.

replace old_name new_name -- ./config/environment.rb ./config/application.rb ./Rakefile ./config/initializers/secret_token.rb ./config/environments/production.rb ./config/environments/development.rb ./app/views/layouts/application.html.erb ./config/routes.rb config.ru ./config/environments/test.rb ./config/initializers/session_store.rb

But be sure to run

fgrep old_name . -iR

Or something similar first to check if there are no occurrences of old_name in your project who not should be replaced.

EDIT:

Of course for this you need to have the replace command installed.

And take in account that your appname will be CamelCased, so maybe you have to try a few different variations, like OldName vs. NewName.



回答6:

You might find yourself having to rename the app when wanting or having to generate a model or scaffold with the same name as the app's name. Just happened to me. Used https://github.com/morshedalam/rename on Rails 3.2.13 and no problems so far.



回答7:

To do this, I have used good old shell commands :

grep -r old_name * | cut -d: -f1 | xargs sed -i .bak 's/old_name/new_name/g'

This command replace the old_name with new_name in all file it finds the old_name. However, it creates a backup file with the extension '.bak'. Those file can be easily removed once you check everything went well.

The advantage of this, is that it is not 'rails version dependent'.



回答8:

On rails 4

Add

gem 'rename'

to Gemfile then do

bundle install

After bundle install

rails g rename:app_to name_of_app