如何更改 Ruby on Rails 应用程序名称?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4383670/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change a Ruby on Rails application name?
提问by Misha Moroshko
I have a Ruby on Rails application that was created using:
我有一个使用以下方法创建的 Ruby on Rails 应用程序:
rails new old_name -d mysql
Now I want to change the application name to be new_name.
现在我想将应用程序名称更改为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_developmentto new_name_development). I wonder if there are other places in the automatically generated files that needs changing.
仅更改应用程序文件夹名称是不够的,因为例如数据库名称也必须更改(从 更改old_name_development为new_name_development)。不知道自动生成的文件有没有其他地方需要修改。
Is there any built in command for changing the application name ?
是否有任何用于更改应用程序名称的内置命令?
采纳答案by Coffee Bite
There's a Rails plugin to do it for you. Super convenient.
有一个 Rails 插件可以为你做这件事。超级方便。
回答by Chris Alley
In Rails 3, there are references to the application name in the following files:
在 Rails 3 中,以下文件中引用了应用程序名称:
- 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
- 配置/应用程序.rb
- 配置/环境.rb
- 配置/环境/development.rb
- 配置/环境/production.rb
- 配置/环境/test.rb
- 配置/初始化程序/secret_token.rb
- 配置/初始化程序/ session_store.rb
- config/mongoid.yml(如果使用 Mongoid)
- 配置/路由.rb
- 配置文件
- 文件
- app/views/layouts/application.html.erb,在标题标签中
回答by Keith Gaddis
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. :)
在 Rails 3 中,应用程序类定义在 config/application.rb 中,并在您的环境文件(config/environment.rb、config/environments/*.rb)和 config/routes.rb 中引用。我认为就是这样,但是如果您错过了一个,您应该很快从 Rails 服务器中找到。:)
- config/application.rb
- config/enviroment.rb
- config/environments/*.rb
- config/routes.rb
- 配置/应用程序.rb
- 配置/环境.rb
- 配置/环境/*.rb
- 配置/路由.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.
也就是说,除非你有一个非常具体的理由想要改变它,否则我不会打扰。不会以任何其他方式真正影响应用程序。
回答by Keith Gaddis
Rails 3 application can rename using https://github.com/morshedalam/rename
Rails 3 应用程序可以使用https://github.com/morshedalam/rename重命名
回答by Koen.
Run the following to replace the old_name for new_name from the root of your Rails (3.0.7) project.
运行以下命令以替换 Rails (3.0.7) 项目根目录中的 new_name 的 old_name。
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.
或类似的东西首先检查您的项目中是否不存在不应替换的 old_name 。
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.
并考虑到您的应用程序名称将是 CamelCased,所以也许您必须尝试一些不同的变体,例如 OldName 与 NewName。
回答by hopper
On rails 4
在轨道上 4
Add
添加
gem 'rename'
to Gemfile then do
到 Gemfile 然后做
bundle install
After bundle install
捆绑安装后
rails g rename:app_to name_of_app
回答by octimizer
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/renameon Rails 3.2.13 and no problems so far.
当想要或必须生成与应用程序名称同名的模型或脚手架时,您可能会发现自己必须重命名应用程序。刚刚发生在我身上。在 Rails 3.2.13 上使用https://github.com/morshedalam/rename,到目前为止没有问题。
回答by Raphael Pr
To do this, I have used good old shell commands :
为此,我使用了很好的旧 shell 命令:
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.
此命令在它找到 old_name 的所有文件中用 new_name 替换 old_name。但是,它会创建一个扩展名为“.bak”的备份文件。一旦您检查一切顺利,就可以轻松删除这些文件。
The advantage of this, is that it is not 'rails version dependent'.
这样做的好处是它不是“依赖于 Rails 版本”。

