升级到 Rails 4,你的应用需要准备什么? 已翻译 100%

oschina 投递于 2013/01/02 10:52 (共 26 段, 翻译完成于 01-06)
阅读 8593
收藏 28
11
加载中
A new Rails version is about to be released and it’s coming with many changes, deprecations, and new features.


Let’s take a look at what you need to do to get your app ready for Rails 4.

已有 1 人翻译此段
我来翻译

The Path to Upgrade

The easiest way to be ready for Rails 4 is having your app on Rails 3.2.
If you are not in 3.2, upgrading should be done by minor versions. That is from 3.0 to 3.1, and then from 3.1 to 3.2.

已有 1 人翻译此段
我来翻译

Deprecations

Some things are going to be completely removed on Rails 4. We should know about them and be prepared for the upgrade.

Ruby 1.9.3+ Only

Rails 4 is going to target only Ruby 1.9.3 and future versions. So make sure that your app runs on it.

If you’re using Ruby 1.9.x, the upgrade should be pretty straightforward.

If you’re still on Ruby 1.8.7, things could be a bit more work.

已有 1 人翻译此段
我来翻译

Nowadays most gems should be running on 1.9 or have a replacement.
Tools like rvm or rbenv help a lot by letting you run multiple versions of Ruby.

Using conditional statements in your Gemfile come in handy for having different gems on each environment until you get it all running on 1.9.

已有 1 人翻译此段
我来翻译

It should look something like this:

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-

A great resource for 1.8 vs 1.9 changes is the Ruby 1.9 walkthrough by Peter Cooper. Strongly recommended!

已有 1 人翻译此段
我来翻译

No More vendor/plugins

Rails 4 will be removing Rails::Plugins class. Therefore, any code in the vendor/plugins directory won’t be loaded.

Most apps were already relying on gems instead of vendored plugins. However, if you still have any code in vendor/plugins, you have 2 options:

  • Move it to a gem. Most plugins should already have a gem version. If not, you can reference it from your Gemfile via the:gitor:pathoptions.
  • Move it tolib/your\_pluginand require it from an initializer onconfig/initializers.

Here’s the commit where this was deprecated.

已有 1 人翻译此段
我来翻译

Route Matching

On routes, thematchmethod will no longer act as a catch-all option. You should now specify which HTTP verb to respond to with the option:via

#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]

Another option for better Rails 3.2 compatibility is to just specify your actions with explicitget,post, or any other HTTP verb. With this option, you still get your code running today and future proof it for the upgrade.

#Rails 3.2 and 4.0 compatible
get "/users/:id" => "users#show"
# multiple verbs
get "/users" => "users#index"
post "/users" => "users#index"

已有 1 人翻译此段
我来翻译
ActiveRecord Scopes Need A Callable object

In Rails 4, all ActiveRecord scopes must be defined with a callable object (likeProcorlambda):

#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') }

This helps avoid subtle bugs with Date or Time objects that get evaluated once, instead of being dynamically evaluated.

已有 1 人翻译此段
我来翻译

Removed Code

A lot of code was also removed from Rails 4 codebase. Don’t worry, though, most is still alive as separate gems. This helps upgraders with smooth transiitons.

Removed code:

已有 1 人翻译此段
我来翻译
A special note for the latter is worth mentioning:

Activerecord-deprecated-finders will be a default dependency on Rails 4.0 to provide the deprecated functionality. But it will removed on 4.1. As such, you should pay close attention to all warnings and start fixing them!

Rails guides provides a helpful explanation on changing most common cases on dynamic finders:

All dynamic methods except for findby… and findby…! are deprecated. Here’s how you can rewrite the code:

  • find_all_by_...can be rewritten usingwhere(...).
  • find_last_by_...can be rewritten usingwhere(...).last.
  • scoped_by_...can be rewritten usingwhere(...).
  • find_or_initialize_by_...can be rewritten usingwhere(...).first_or_initialize.
  • find_or_create_by_...can be rewritten usingfind_or_create_by(...)orwhere(...).first_or_create.
  • find_or_create_by_...!can be rewritten usingfind_or_create_by!(...)orwhere(...).first_or_create!.

All these gems will help having a smooth transition. I’d recommend that, for a full Rails 4 experience, just use them in development allowing the warning messages to help you get all your code up to the latest syntax.

已有 1 人翻译此段
我来翻译
本文中的所有译文仅用于学习和交流目的,转载请务必注明文章译者、出处、和本文链接。
我们的翻译工作遵照 CC 协议,如果我们的工作有侵犯到您的权益,请及时联系我们。
加载中

评论(9)

Zoker
Zoker
解决了我的路由匹配上的问题,多谢!
tianshuai
tianshuai
收藏了
latpaw
latpaw
最近用rails4感觉很坑,看了这篇就了然了.
C
ChenRain
首先肯定下译者的辛苦工作,但不得不吐下槽。
首先感觉不是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里的关键词)都应该直接用原名,不要加中文翻译。

C
Charlsi
不错,收藏一下。
红薯
红薯

引用来自“Tom Lin”的评论

奇怪,还没翻译完,就到首页上去啦?

是啊,鼓噪大家一起来完成它!:)
王振威
王振威
不错
Tom-Lin
Tom-Lin
奇怪,还没翻译完,就到首页上去啦?
红烧土豆
红烧土豆
感谢楼主。
返回顶部
顶部