简体   繁体   English

如何更改Ruby on Rails应用程序名称?

[英]How to change a Ruby on Rails application name?

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_development to new_name_development ). 仅更改应用程序文件夹名称是不够的,因为例如,还必须更改数据库名称(从old_name_development更改为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 ? 是否有用于更改应用程序名称的内置命令?

In Rails 3, there are references to the application name in the following files: 在Rails 3中,以下文件中引用了应用程序名称:

  • config/application.rb 配置/ application.rb中
  • config/environment.rb 到config / environment.rb
  • config/environments/development.rb 配置/环境/ development.rb
  • config/environments/production.rb 配置/环境/ production.rb
  • config/environments/test.rb 配置/环境/ test.rb
  • config/initializers/secret_token.rb 配置/初始化/ secret_token.rb
  • config/initializers/session_store.rb 配置/初始化/ session_store.rb
  • config/mongoid.yml (if using Mongoid) config / mongoid.yml(如果使用Mongoid)
  • config/routes.rb 配置/ routes.rb中
  • config.ru config.ru
  • Rakefile Rake文件
  • app/views/layouts/application.html.erb, in title tag app / views / layouts / application.html.erb,标题标记

There's a Rails plugin to do it for you. 有一个Rails插件可以帮你完成。 Super convenient. 超级方便。

https://github.com/get/rename https://github.com/get/rename

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. 在Rails 3中,应用程序类在config / application.rb中定义,并在您的环境文件(config / environment.rb,config / environments / * .rb)和config / routes.rb中引用。 I think that's it, but you should find out pretty quickly from rails server if you missed one. 我认为就是这样,但如果错过了一个,你应该很快从rails服务器中找到它。 :) :)

  • config/application.rb 配置/ application.rb中
  • config/enviroment.rb 配置/ enviroment.rb
  • config/environments/*.rb 配置/环境/ *。RB
  • config/routes.rb 配置/ 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. 不会以任何其他方式影响应用程序。

Rails 3应用程序可以使用https://github.com/morshedalam/rename重命名

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. 当然,为此您需要安装replace命令。

And take in account that your appname will be CamelCased, so maybe you have to try a few different variations, like OldName vs. NewName. 并且考虑到您的appname将是CamelCased,因此您可能需要尝试一些不同的变体,例如OldName vs. NewName。

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. 在Rails 3.2.13上使用https://github.com/morshedalam/rename ,到目前为止没有问题。

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. 此命令在找到old_name的所有文件中将old_name替换为new_name。 However, it creates a backup file with the extension '.bak'. 但是,它会创建一个扩展名为“.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'. 这样做的好处是它不是“轨道版本依赖”。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM