简体   繁体   English

我怎么知道gem与某个版本的rails兼容?

[英]How do I know that a gem is compatible with a version of rails?

I'm just getting started with ruby on rails and as I follow the tutorial, there isn't an explanation of how certain gems were gathered and placed in the Gemfile. 我刚开始使用ruby on rails并且按照教程,没有解释如何收集某些宝石并放置在Gemfile中。 I've just been copying and pasting them and putting them in my Gemfile and running bundle install. 我刚刚复制并粘贴它们并将它们放入我的Gemfile并运行bundle install。

How does one go about downloading specific versions of gems, and their dependencies as well as making sure that they are compatible with the version of rails I'm using? 如何下载特定版本的gem及其依赖项,以及确保它们与我正在使用的rails版本兼容?

Bundler figures out which versions to install depending on your current Rails version if you don't specify the Gem version. 如果您没有指定Gem版本,Bundler会根据您当前的Rails版本确定要安装的版本。 Usually Bundler will warn you also when it can't install a version you specified. 通常Bundler会在无法安装您指定的版本时发出警告。

gem 'gemname'

This installs whatever version is compatible with your Rails version. 这将安装与您的Rails版本兼容的任何版本。

gem 'gemname', '1.5'

This installs version 1.5 only if it supports your current Rails version. 仅当它支持您当前的Rails版本时,才会安装1.5版。

gem 'gemname',  '>=1.0'

This installs version 1.0 or greater if available and compatible. 如果可用且兼容,则安装1.0或更高版本。

If you want to install a specific version (2.2) but you know that version 3.0 will break your code (some gems do that like Mailchimp gem) you can specify a minimum version and maximum version: 如果你想安装一个特定的版本(2.2),但是你知道版本3.0会破坏你的代码(一些宝石就像Mailchimp gem那样),你可以指定最低版本和最高版本:

gem 'gemname', '>= 2.2.0', '< 3.0'

Since it is more or less common there is a shortcut for this: 由于它或多或少常见,因此有一个快捷方式:

gem 'gemname', '~> 2.2'

The "~>" will match any version smaller than 3.0. “〜>”将匹配任何小于3.0的版本。 It tells bundler to install only 2.X never reaching 3.0. 它告诉bundler只安装2.X永远不会达到3.0。

Every gem you want to install will tell you which version is compatible with your Rails version. 您要安装的每个gem都会告诉您哪个版本与您的Rails版本兼容。 Usually it will say the minimum version number. 通常它会说最小版本号。 For example the boostrap gem: 例如,boostrap gem:

https://rubygems.org/gems/bootstrap/versions/4.0.0.alpha3.1 https://rubygems.org/gems/bootstrap/versions/4.0.0.alpha3.1

If you look at the site, it tells you the dependencies. 如果您查看该站点,它会告诉您依赖项。 It doesn't mention a minimum Rails version so you can install always the latest version: 它没有提到最低Rails版本,因此您可以始终安装最新版本:

RUNTIME DEPENDENCIES (2):
autoprefixer-rails >= 6.0.3
sass >= 3.4.19
DEVELOPMENT DEPENDENCIES (13):
actionpack >= 4.1.5
activesupport >= 4.1.5
capybara >= 2.6.0
compass ~> 1.0.3
jquery-rails >= 3.1.0
json >= 1.8.1
minitest ~> 5.8.0
minitest-reporters ~> 1.0.5
poltergeist >= 0
slim-rails >= 0
sprockets-rails >= 2.3.2
term-ansicolor >= 0
uglifier >= 0

If it specifies a Rails version under dependencies like this: 如果它在依赖关系下指定Rails版本,如下所示:

rails >= 4

It means that you need at least Rails 4.0. 这意味着您至少需要Rails 4.0。

For rails 4 and 5 you can check here . 对于导轨4和5,您可以在这里查看 If gem ready or not. 如果宝石准备与否。

Rails automatically install best suited newest version of gem when you run bundle install if you write gem 'gemname' . 如果你编写gem 'gemname' ,当你运行bundle install时,Rails会自动安装最适合的最新版gem。 Specifying a gem version is mention in other answers. 在其他答案中提到指定gem版本。

If a specific version of ruby or rails or any dependency is required in a gem. 如果gem中需要特定版本的ruby或rails或任何依赖项。 Then it is specified in gemspec file of gem and Gemfile for that gem. 然后在gem的gemspec文件和gemfile中为该gem指定。

You can cross-check in gemspec or Gemfile of gem also if something break while bundle install. 如果在安装捆绑包时出现问题,您也可以交叉检查gemspec或gem的Gemfile。

In your gemfile you can list gems as follows: 在gem文件中,您可以按如下方式列出gem:

To load the latest stable version just leave the version out 要加载最新的稳定版本,请保留版本

gem 'gemname'

To load a specific version, add the version 要加载特定版本,请添加版本

gem 'gemname', '1.0.0' # or whatever version you want to specify

To load a specific version or whatever the highest subversion is 加载特定版本或最高版本的subversion

gem 'gemname', '~> 1.0.0' # this will load version 1.0.x with x being the highest sub-version.
gem 'gemname', '~> 1.0' # will load version 1.x
gem 'gemname', '~> 1.3' # will load version 1.3 or higher but less than 2.0

You can update the gem versions in your gemfile based on how you listed them (per the above) by running bundle update. 您可以通过运行bundle update来根据您列出它们的方式更新gemfile中的gem版本(按照上述内容)。

To know whether a version is compatible with your Rails version I generally look at the date of the release on rubygems.org compared to the release date of the latest major Rails version. 要知道版本是否与您的Rails版本兼容,我通常会在rubygems.org上查看发布日期与最新主要Rails版本的发布日期。 A gem released between June 2013 and June 2016 would most likely be targeting Rails 4. It may or may not work in Rails 5 (which was released June 2016). 2013年6月至2016年6月期间发布的宝石很可能是针对Rails 4.它可能会也可能不会在Rails 5(2016年6月发布)中发挥作用。 I find that most gems that work for Rails 4 work in Rails 5, and Rails 5 has been out long enough for issues with a specific gem compatibility to be already documented. 我发现大多数适用于Rails 4的宝石都可以在Rails 5中运行,而Rails 5已经足够长,可以解决具有特定宝石兼容性的问题。 Gems with the last release date prior to June 2013 (so for Rails 3 or earlier) I avoid. 2013年6月之前的最后发布日期的宝石(对于Rails 3或更早版本)我避免使用。

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

相关问题 如何在项目中更新Rails的版本? Bundler找不到宝石“ activemodel”的兼容版本 - How do I update the version of Rails in my project? Bundler could not find compatible versions for gem “activemodel” 如何修复“找不到与宝石“导轨”兼容的版本”? - How to fix 'could not find compatible version for gem “rails”'? Ruby on Rails 2.3.8:我如何需要特定版本的gem? - Ruby on Rails 2.3.8: How do I require a specifc version of a gem? 我如何从 jquery-ui-rails gem 版本中找出 JQuery UI 的版本? - How do i figure out the version of JQuery UI from version of jquery-ui-rails gem? 如何设置Rails项目以使用Edge的边缘源树(不是已安装的gem)版本 - How do I setup a rails project to use an edge source tree (not an installed gem) version of rails 如何停用特定版本的gem? - How do I deactivate a specific version of gem? Rails 5:我如何找出哪个 Gem 取决于特定的 Ruby 版本? - Rails 5: How Do I Find Out Which Gem Depends on a Specific Ruby Version? Bundler找不到宝石“ rails”的兼容版本,但已安装版本 - Bundler could not find compatible versions for gem “rails”, but version is installed 如何调试rails 3 gem? - How do I debug a rails 3 gem? Rails:如何包含gem依赖项? - Rails: How do I include gem dependencies?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM