62

rails plugin install git://github.com/get/Rename.git will allow us to rename only rails 3 app

Is there any gem available to rename Rails 4 app.

If not, suggest me the better way to rename.

Arun Kumar Kandasamy
  • 1,016
  • 2
  • 12
  • 24
  • 2
    possible duplicate of [Renaming Ruby on Rails application](http://stackoverflow.com/questions/3270373/renaming-ruby-on-rails-application) – Intrepidd Jan 08 '14 at 08:37

11 Answers11

135

Since rails 4.1.x, if you want to rename your application, the only two files you need to modify are config/application.rb:

require File.expand_path('../boot', __FILE__)

require 'rails/all'

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)

module YourApplicationName # <-- rename it here
   class Application < Rails::Application
     ...
   end
end

and config/initializers/session_store.rb (optional):

# Be sure to restart your server when you modify this file.

Rails.application.config.session_store :cookie_store, key: '_your_application_name_session' # <-- rename the key

For Rails 4.0.x you can use the rename gem and execute the following command:

rails g rename:app_to New-Name

This will update the necessary files for you:

old/ (master)  › rails g rename:app_to new
Search and replace module in to...
    gsub  config.ru
    gsub  Gemfile
    gsub  Gemfile.lock
    gsub  Rakefile
    gsub  README.md
    gsub  config/application.rb
    gsub  config/boot.rb
    gsub  config/environment.rb
    gsub  config/environments/development.rb
    gsub  config/environments/production.rb
    gsub  config/environments/test.rb
    gsub  config/initializers/backtrace_silencers.rb
    gsub  config/initializers/filter_parameter_logging.rb
    gsub  config/initializers/inflections.rb
    gsub  config/initializers/load_class_extensions.rb
    gsub  config/initializers/mime_types.rb
    gsub  config/initializers/secret_token.rb
    gsub  config/initializers/session_store.rb
    gsub  config/initializers/update.rb
    gsub  config/initializers/wrap_parameters.rb
    gsub  config/routes.rb
    gsub  config/initializers/session_store.rb
Renaming references...
Renaming directory...Done!
New application path is '/Users/username/code/new'
Daniel
  • 4,082
  • 3
  • 27
  • 46
  • 1
    The Rails 4.1.x example didn't work for me using Rails 4.1.0. I found that I had to change the name of the module in config/routes.rb as well: `YourApplicationName::Application.routes.draw do`. – Adam Jul 19 '14 at 05:57
  • 1
    @Adam this is true if you upgrade an existing Rails application to 4.1.x. Running `rake rails:update` changes `YourApplicationName::Application` to `Rails.application`. – Daniel Jul 20 '14 at 10:28
  • I agree with the 2 files @Daniel mentions for a Rails 4.2.0.beta2 app (`config/application.rb` and `config/initializers/session_store.rb`). You may also find a reference to the app name in the title of `app/views/layouts/application.html.erb`. – Eliot Sykes Nov 01 '14 at 11:59
  • 4
    You might want to rename the databases in `config/database.yml` too. – yenshirak Nov 07 '14 at 18:37
  • 3
    If you follow @yenshirak's advice to rename the database, note that changing the name in `database.yml` only changes the place that it looks for the database, it does not actually rename the database itself. If you change the specified location, you'll also need to rename the database using a tool like `pgadmin` or `psql`. IMO, it's easiest to leave this unchanged. – Dennis Feb 09 '15 at 17:08
  • I tried to use it with Rails 4.2.0 and the gem also updated the 2 files (session_store.rb and application.rb) :) – imtk Jan 04 '16 at 15:28
  • This answer does not change the name on the browser tab. – jasonleonhard Feb 21 '17 at 05:52
  • Such as view titles and other breadcrumbs of the previous name. – jasonleonhard Feb 21 '17 at 05:55
  • 1
    @jasonleonhard the browser tab title is provided by your layouts `title` tag. Check `app/views/layouts/application.html.erb` if you want to change it. – Daniel Jun 08 '17 at 14:31
36

Add

gem 'rename' to Gemfile

then

bundle install

After that

rails g rename:app_to name_of_app

And if you are using mongoid then you need to rename the database name in config/mongoid.yml

Ajay Barot
  • 1,681
  • 1
  • 21
  • 37
Sushant Mane
  • 757
  • 7
  • 14
  • 5
    This currently accepted answer is near identical to the person who posted it first in 2014: @complistic – jasonleonhard Sep 01 '15 at 19:51
  • While this gem works, it should be used with caution. I had an experimental application named admin. When I used rename gem to change its name it also changed all occurences of Admin in comments as well as code to the new name many of which were in fact related to Active Admin. It even changes constants of which the project name just happens to be a part of, like : `ActiveAdmin` – lorefnon Feb 13 '16 at 16:26
  • [DEPRECATION] `app_to` is deprecated. Please use `into` instead. – Breno Dec 27 '18 at 16:07
29

There are two ways:

1 . Manually (For Rails 4.1.x)

You need to manually find the references to the application name. And you need to change them manually. Here is some common places where it is used:

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/routes.rb
config.ru
    app/views/layouts/application.html.erb
Rakefile

2 . Automatic (For Rails 3 and 4.0.X)

Or you can use the rename gem and execute the following command:

rails g rename:app_to New-Name
Hardik
  • 3,815
  • 3
  • 35
  • 45
  • Note that the rename gem README says "This plugin will only work for Rails 3 and below." – Dennis Feb 09 '15 at 17:00
  • I was able to rename my rails application( rails 4.0.4 ) using this gem... Rails 4.1 and later are not supported by this gem. – Hardik Feb 16 '15 at 14:03
23

For Rails 5

Require

  • config/application.rb change the module name

Optional

  • config/initializers/session_store.rb (in Rails.application.config.session_store) change the session name
  • app/views/layouts/application.html.erb You can change the <title>...</title>, if it is not already done
Fabien Sa
  • 9,135
  • 4
  • 37
  • 44
16

I just used this rename gem in a basic rails 4 app:

https://github.com/morshedalam/rename

This is quite a bit different to get's version.


Easy enough to use:

Add this to Gemfile:

gem 'rename'

And run:

rails g rename:app_to NewName

Seemed to the trick,
It also updated my rubymine .idea project settings :)

complistic
  • 2,610
  • 1
  • 29
  • 37
  • What is "get's version"? – user229044 Mar 19 '14 at 04:10
  • The https://github.com/get/rename `plugin` stated in the question and other answer is by the github user `get`, this is completely different to the https://github.com/morshedalam/rename `gem` im talking about, and wanted to make that a bit more obvious. (the later works in Rails 4) – complistic Mar 19 '14 at 04:14
9

In Rails 4.2 just change in application config file

config/application.rb

and config/initializers/session_store.rb (optional):

Rails.application.config.session_store :cookie_store, key: '_your_application_name_session' # <-- rename the key

then restart your server.

That's it!

Manish Shrivastava
  • 30,617
  • 13
  • 97
  • 101
1

For rails 5.2

Add gem 'rename' to the gemfile

bundle install

rails g rename:into your_new_app_name

stevec
  • 41,291
  • 27
  • 223
  • 311
0

Here is a gem specifically for Rails 4 https://github.com/negativetwelve/rails-rename (I haven't used it but it seems fine)

The other gems listed here only target Rails 3

0

In Rails 5.x, doing it manually

using ag (https://github.com/ggreer/the_silver_searcher), there are files that use the default folder name (if generated via rails new .)

three basic files, relative to root of project, need to be updated:

  • config/cable.yml > channel_prefix:
  • config/environments/production.rb > # config.active_job.queue_name_prefix
  • package.json > name

There are sure to be more locations as mentioned previously such as database etc. Hope this helps.

mirageglobe
  • 2,446
  • 2
  • 24
  • 30
0

How to Rename a Rails 6.1.3 Application

the "rename" gem doesn’t appear to be tested against Rails 6 at this time according to their documentation so I opted for the manual route.

  • config/database.yml (rename the 3 databases)
  • config/application.rb (rename the module)
  • package.json (rename the package name)
  • README.md (in case it mentions your old app name)
  • app/views/layouts/application.html.erb (rename the title)
  • config/cable.yml (name the Channel_prefix database to the new production database)
  • Run npm install so that the name attribute you previously updated in package.json is registered.
  • Run your migrations rails db:migrate
  • Start your server rails s
random_user_0891
  • 1,863
  • 3
  • 15
  • 39
0

For Rails 6.1 Here is what I did: rename module config/application.rb run bundle install and run rails db:migrate.

after that running rails c then Rails.application.class.module_parent.name

Asme Just
  • 1,287
  • 5
  • 27
  • 42