例如:
gem 'ruby18-only-gem', :platforms => :ruby_18 gem 'ruby19-only-gem', :platforms => :ruby_19 #More on this can be seen in the Bundler manpages: #http://gembundler.com/man/gemfile.5.html#PLATFORMS-platforms-
关于 1.8 和 1.9 之间的改变可看 Peter Cooper 写的这篇文章 the Ruby 1.9 walkthrough . 强烈推荐!
Rails 4 将删除 Rails::Plugins 类,所以将不会再加载 vender/plugins 目录下的任何代码。
大多数应用应该依赖于 gems 而不是插件。但如果你在 vender/plugins 中还有一些代码,你有两种选择:
这里是关于废弃插件的说明 commit
关于路由,匹配方法不再作为是 catch-all 选项,你可以指定需要响应什么 HTTP 方法,包括 GET/POST 之类的。
#Rails 3.2 match "/users/:id" => "users#show"
#Rails 4.0 match "/users/:id" => "users#show", via: :get #or specify multiple verbs match "/users" => "users#index", via: [:get, :post]
另外一个更好兼容 Rails 3.2 的方法是显式的指定 HTTP 方法,是 GET 或者 POST,或者是其他的方法。这样你就可以兼容现在的版本和 Rails 4.0.
#Rails 3.2 and 4.0 compatible get "/users/:id" => "users#show" # multiple verbs get "/users" => "users#index" post "/users" => "users#index"
ActiveRecord 范围需要一个 Callable 对象。
在 Rails 4 中,所有 ActiveRecord 范围必须使用一个 callable 对象来定义:
#Rails 3.2 scope :recent, where(created_at: Time.now - 2.weeks)
#Rails 4 scope :recent, -> { where("created_at > ?", Time.now - 2.weeks).order("created_at desc") } scope :active, -> { where(status: 'active') }
这个可避免一些关于日期和时间对象的微小 bug,无需动态的评估。
在 Rails 4 中有很多代码会被删除,别担心,因为大多数被删除的代码还会作为独立的 gems 存在,这样有助于更平滑的迁移。
被删除的代码包括:
特别值得一提的注意事项:
Activerecord-deprecated-finders 是 Rails 4.0 中默认提供废弃功能的依赖包,但它也将在 4.1 版本中被删除。因此你需要密切关注所有的警告信息,并开始修复这些警告。
Rails 指南 提供一个很有用的解释,关于在大多数情况下如何修改动态查找器:
所有动态的方法除了 findby… 和 findby…! 外都已废弃,你可以使用如下的替代方法:
- find_all_by_...改为 where(...).
- find_last_by_...改为 where(...).last.
- scoped_by_...改为 where(...).
- find_or_initialize_by_...改为 where(...).first_or_initialize.
- find_or_create_by_...改为 find_or_create_by(...) 或者 where(...).first_or_create.
- find_or_create_by_...!改为 find_or_create_by!(...) 或者 where(...).first_or_create!.
所有的那些 gems 都可以帮你实现平滑的迁移。我的建议是:对一个完全的 Rails 4 环境,通过警告信息来帮你的代码适应于最新版本的语法。
评论删除后,数据将无法恢复
评论(9)
首先感觉不是rails使用者翻译的(频繁出现的“评估”,其实就是常见的eval啊),
This helps avoid subtle bugs with Date or Time objects that get evaluated once, instead of being dynamically evaluated.
这应翻译为:这有助于避免使用Date或Time对象时只执行一次而不是动态执行这类的隐性问题。
另外,所有gem(以及所有rails里的关键词)都应该直接用原名,不要加中文翻译。
引用来自“Tom Lin”的评论
奇怪,还没翻译完,就到首页上去啦?