Gemfile在rails中有什么用? [英] What is the use of Gemfile in rails?

查看:58
本文介绍了Gemfile在rails中有什么用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Gemfile 在 rails 中有什么用?

What is the use of Gemfile in rails?

如何使用Gemfile?

推荐答案

在 Rails 开发过程中,有时您会想要提供一些您需要的功能,但是您不知道如何提供做或您不想自己实现它,因为有才华的开发人员已经投入了大量工作来开发它.

During your development in Rails, there will be times where you will want to provide some functionality which is required by you, but either you don't know how to do or you don't want to implement it on your own since a lot of work has been put into its development by talented developers.

您可能需要的这些开发(用户身份验证、消息系统、资产处理程序、地理定位、分页系统、链接到外部服务,如 Amazon AWS,以及最后但并非最不重要的 Rails 本身)被称为 Ruby Gems.这些是 ruby​​ 软件包,不一定与 Rails 相关,但由于 Rails 基于 Ruby,因此 98% 的 gem 可用于您的 Rails 网络应用程序代码.

These developments which you might need (user authentication, message system, asset handlers, geolocation, pagination system, linking to exterior services such as Amazon AWS, and last but not least Rails itself) are called Ruby Gems. These are ruby software packages, not necessarily relating to Rails, but since Rails is based on Ruby, 98% of the gems can be made availble to your Rails webapp code.

github 中可以找到很多 gem,但通过 ruby-gemsruby-toolbox

Lots of gems can be found in github, but its funner to search for gems via ruby-gems or ruby-toolbox

您的 gemfile 是您想要包含在项目中的所有 gem 的列表.它与 bundler(也是一个 gem)一起使用来安装、更新、删除和以其他方式管理您使用的 gem.

Your gemfile is a list of all gems that you want to include in the project. It is used with bundler (also a gem) to install, update, remove and otherwise manage your used gems.

gemfile 还有另一个用途——你可以在 :development:test:assets 中对 gem 进行分组、:production 等组和 Rails 会知道何时包含 gem.例如:

The gemfile has another purpose - you can group gems in :development, :test, :assets, :production, etc groups and Rails will know when to include the gems. For example:

group :development, :test do
    gem "rspec-rails"
    gem "factory_girl_rails"
    gem "guard-rspec"
end

请注意,在 Rails 4 上,assets 组已被弃用

Note that on Rails 4, the assets group has been deprecated

这些 gem 属于开发环境和测试环境,因为它们用于测试应用程序.您不需要它们在生产环境中可用(您可以,但这会不必要地增加内存).

These gems belong to development environment and the test environment since they are for testing the application. You don't need them available in the production environment (you could, but that will bloat the memory unnecessarily).

所以 - 要使用 gemfile,只需编写您希望安装的 gem,例如

So - To use the gemfile, simply write the gem you wish to install such as

gem 'devise'

确保使用

$ gem install bundler

然后在控制台写

bundle install

你会注意到另一个 gemfile 出现了!Gemfile.lock如果您使用文本阅读器打开它,您将看到该文件列出了所有 gem 及其版本和依赖项.当您需要知道您安装了哪些版本的 gem 时,这会很有用.

you will notice another gemfile appears! Gemfile.lock This file, as you will see if you open it with a text reader, lists all your gems with their version and their dependencies. This will come useful when you need to know which versions of the gems you installed.

有关 Gemfile 的更多阅读 - 阅读打包程序页面

For more reading on the Gemfile - read on the bundler page

有关挑选宝石的信息,您可以从这个开始

for information regarding picking a gem you could start with this

祝你好运,玩得开心!

好的,那么创建的这个 Gemfile.lock 是什么?

Gemfile.lock,顾名思义是对已安装的所有 gem 的所有版本的锁定.因此,如果 Gemfile 是需要安装的文件,那么锁定文件就是安装的文件以及启动和运行应用程序实际需要的版本.

Gemfile.lock, as the name suggests is a locking on all the versions of all the gems that got installed. So if Gemfile is what required to be installed, the lock file is what got installed and what version are actually required to get the app up and running.

如果你没有那个特定版本的 gems(如 Gemfile.lock 中指定的),rails 会报错,你必须安装缺少的 gems(通过 bundle install)或修复任何手动冲突(我相信 bundler 会给你一些线索)

If you don't have the gems in that specific version (as specified in Gemfile.lock) rails will complain and you will have to either install the missing gems (via bundle install) or fix any conflicts manually (I believe bundler will give you some clues on that)

关于Gemfile.lock

  • 如果您不小心删除了它,它会在您运行 bundle install 时重新生成.如果你不小心删除了 Gemfile,那你就不走运了.. 你应该使用 git :)
  • Heroku 不关心 Gemfile.lock,因为它会重新安装所有 gem.所以对于 Heroku,你必须设置你想要的 gem 版本,否则 Heroku 会一直安装最新版本的 gem,这可能会导致问题
  • 将 Gemfile.lock 保留在您的项目中,以便您始终知道哪个版本的 gem 使您的应用正常运行.
  • if you accidently delete it, it will get regenerated when you run bundle install. If you accidently delete Gemfile, you are out of luck.. You should use git :)
  • Heroku doesn't care about Gemfile.lock since it will reinstall all gems. So for Heroku, you must set the gem version you want, or Heroku will always install the latest version of gem, which may cause issues
  • Keep the Gemfile.lock in your project so you will always know what version of gems make your app work properly.

这篇关于Gemfile在rails中有什么用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆