加载中

Rails 5 is right around the corner (currently targeting Fall 2015) and there are some exciting features coming up. If you are running a Rails shop, you need to prepare your apps for this major release.

Don’t worry. As always, we at Sitepoint will guide you throughout the migration process when the release date approaches. For now, let’s catch you up on what’s coming and how it will improve your development process.

Major Improvements

There are some amazing announcements that will fundamentally shift how we work with Rails. New features like Action Cable and improved Turbolinks that can instantly improve our web development workflow. Let’s look at each of them in detail.

Rails 5 即将在 2015 年秋季发布,新版本拥有很多令人激动的特性。如果你使用 Rails 开发商城,那你必须使你的应用准备好迎接新的 Rails 新特性。

别担心!我们会在新版本到来之前让你了解新特性,现在就让我们让你了解有哪些新特性,这将会大大提高你的开发能力。

主要改进

有很多新特性将从根本上改变我们使用 Rails 的方式。比如说 Action Cable 和改进的 Turbolinks 新特性,能够立马提高我们开发 Rails 应用的工作流程。让我们看一些这两个新特性的细节。

Merging Rails API

Over the last few years, many thanks to Backbone.js and Angular.JS, the number of Single Page Applications (SPAs) are on the rise. The last few projects that I have built have been purely SPA and using Rails for these cases was an overkill. I tried dabbling with Sinatra and even pure Rack applications, but ended up writing too much boilerplatee. The routing was lacking compared to Rails and there were too many security vulnerabilities that I had to handle.  I had to either write it on top of the community driven rails-api gems or live with traditional Rails.

In Rails 5, the rails-api gem will be merged into core allowing the use of Rails as a simple JSON API. Personally, I find this a great addition. I won’t have to look further while building an API for my javascript (and other) appliciation clients.

合并 Rails API

在过去的几年中,得益于 Backbone.JS 和 Angular.JS 类库,单页面应用程序(SPA)得到了发展。我在近期开发的几个单页面应用程序借助了 Rails 的威力。我曾经尝试涉猎 Sinatra 项目甚至纯粹的 Rack 项目,但是最终的结果都是写了大量 的boilerplatee 项目。我的做法没有参照 Rails,而且需要处理很多安全缺陷。我不得不在 rails-api 的社区驱动上层和传统的 rails 上进行开发。

在 Rails 5 中,rails-api 被整合到核心包中,并且能够像简单 JSON API 那样子使用 Rails。结合个人经历,我认为这是一个伟大的改进。我不再用为我的 JavaScript 客户端建立后台 API 发愁了。

Ruby 2.2.1

Ruby 2.2 was a great addition to the Ruby community. Not only did it bring a huge performance boost to Ruby, but it also introduced a slew of new functions. In addition, garbage collection of symbols was widely celebrated. Legend has it that Dragons of Valeria rained down rose petals on the Seventh Kingdom when this announcement was made. OK! Maybe I took that too far, but you get the general picture.

NOTE: Before migrating to Rails 5 you need to make sure that your app works on Ruby 2.2.1.

Due to these enhancements, Rails 2.2+ was a ripe candidate for Rails 5. Rails 5 will only work on Ruby 2.2.1 and above.

Ruby 2.2.1

Ruby 2.2 是 Ruby 社区的一个新版本。不仅仅带来了Ruby巨大的性能提升,还引入了大量的新功能。此外,symbols的垃圾收集获取了广泛的好评。有这样一个传说,当这个声明发布的时候,龙族的Valeria在Seventh王国落下了玫瑰雨。OK!也许我扯得太远了,但是一般来说是这样的。

注意:在迁移你的Rails 5之前你需要确认你的应用可以在Ruby 2.2.1上工作

由于这些增强功能,Ruby 2.2+对Rails 5来说是一个可靠的选择。Rails 5将仅支持在Ruby 2.2.1以上的版本上工作。

Turbolinks 3

Traditional web apps tend to be slow due to full page reloads. One way to solve this is just reloading the bare minimum content area. Turbolinks solve this by just reloading the contents of the body from the server, instead of refreshing the entire page. While this improves the performance to a certain extent, this is also relatively slow. Turbolinks 3 aims to solve this.

Tubolinks 3 allows you to retain most of your page and selectively update certain regions through partials. This is very similar to how SPA’s work, and you can choose to do all this from the server. Sounds great, right?

Turbolinks 3

传统的 web 应用一般整页加载,而导致响应比较慢。解决这个问题的一个办法是只加载最小的内容区域。 Turbolinks 只从服务器重载某个区域而不是刷新整个页面。 这个改进虽然一定程度上改进了性能,但是相对还是比较慢。Turbolinks 3 将解决这个问题。

Tubolinks 3 让你可以有选择性的更新部分区域(通过 partial),而大部分页面都不更新。这很像 SPA的工作。你可以选择直接在 server 端完成所有这样的工作,是不是很棒?

This is a great feature as the flock moves towards single page apps. However, at first glance, it looks like you’ll have to manage the partials manually. This means that you’ll have to remember which part of the app to reload at a specific point. To me, it adds a lot of clutter to my code and makes it easy to screw up. Personally, I prefer using something like React, which handles this very well with the Virtual DOM.

Note: The way React works is, it maintains a Virtual DOM where all the manipulations happen. It then diff’s the Virtual DOM and the actual DOM and makes only the required changes. It ensures that DOM is hit only when absolutely required and the changes are bare minimum.

Having said that, for the folks who don’t like to meddle around with JS, this offers an instantaneous performance boost.

这是一个很赞的特性,目前的趋势朝着单页应用发展。然而,乍一看,它好像需要你去手动管理。这意味着你必须在特定的点上记住程序需要重载的部分。对我来说,这增加了不少代码累赘,而且容易搞砸。就我个人而言,我更喜欢使用React类似的东西,它通过虚拟DOM处理这个很在行。

备注: React的工作方式是:它维护了一个所发生操作的虚拟DOM。然后,比较虚拟DOM和实际DOM的差异,然后只做需要的变更。它确保DOM只有在绝对必要才发生变化且是最小化的。

不得不说的是,对于那些不喜欢围着JS转的人,这提供了一个即时的性能提升。

Action Cable

Many projects these days use WebSockets to push live updates to the client. While most client browsers have started to support this, we still need a robust client on the server to manage the subscribers and send an update signal appropriately. This feature is available out of the box in some of the newer frameworks, like Phoenix for Erlang. However, the Rails community had to resort to third party implementations, like Pushr, to get this working.

Note: For the uninitiated, WebSockets is a W3C standard that opens up a duplex connection from your browser. Servers can talk (i.e., push updates) to clients as needed, and asynchronously update the client’s state without a full page reload. For instance, this is how Gmail loads new emails without reloading the entire page.

Rails traditionally offers all the tools that one needs to build a great app out of the box. That’s one reason why it is so popular, especially among the startup community. The lack of WebSocket support was a reason for major discontent among the community. It looks like the Rails core team took note of this and came up with Action Cable. We’ll have to wait for some time to see how this is going to work in real life, but it’s exciting nevertheless.

Action Cable

现在需要项目使使用 Websockets 来推送实时更新到客户端。虽然大多数浏览器已经开始支持这一点,我们任然需要服务器上一个强大的客户端来,管理用户和适当的发送更新信号。这种功能在一些新的框架中已经出现,像 Phoenix for Erlang。Rails 社区需不得不需要求助于第三方解决方案来实现这个,像Pushr.

    注意:对于新手来说,Websockets从浏览器开启双工连接是W3C的标准.服务器端可以在需要的时候和客户端通信(比如推送更新)并且同步更新客户端状态而不需要整个页面重新加载.举例来说,这就是Gmail如何在不重新加载真个页面的情况下加载新的邮件.

通常 Rails 都提供了所有需要建立应用的开箱即用的工具。这也是它为什么如此流行的原因,特别是在一些初创团队。对 Websocket 支持的缺乏是社区中主要的不满原因之一。看起来 Rails 核心团队已经注意到这一点并且开始讨论Action Cable。我们不得不等待一段时间看它是如何在现实生活中工作的,但它任然令人振奋。

Some Niceties

Rake Inside Rails

For many a Rails noob, having to figure out when to use rake and when to use rails is a source of confusion. Now you don’t need to switch context between the rake and rails commands. You can run all Rake tasks with the rails keyword. For instance,

rake db:migrate

will now become:

rails db:migrate

This may not look like much on the surface, but this will make the lives of beginners much simpler. Rails 5 will also add a restart command that quickly restarts the application.

一些新的细节

Rails 内置 Rake

对于许多 Rails 新手来说,困惑的原因来自于不得不想出什么时候是有 rake 什么时候使用 rails。现在你不再需要在 rake 和 ralis 命令的上下文直接切换。你可以使用 ralis 命令运行所有的 Rake 任务。比如说:

rake db:migrate

将会变成:

rails db:migrate

这表面上看起来不太相同,但是对于许多初学者来说会变得简单。Rails 5 也会添加 restart 命令来快速重启应用。

Active Record Changes

One change that almost went unnoticed was the update to belongs_to. When you create a Student belongs_to Class relation, it was possible to create a student without an associated Class relation. This lead to a lot of data inconsistencies. With Rails 5, he parent has become mandatory. If you try to insert an empty record here, Active Record will throw up.

Changes to Controller Tests

If you’re testing what your template renders in your controller, you are doing it wrong. A simple change to your template will leave you with sleepless nights, hunting down the culprit. That’s why, in Rails 5, assert_template is deprecated. However, you can continue using assert_select to check if a specific DOM element is present.

If you’ve been testing the instance variables inside a controller method, you should note that assigns is also going away.

活动记录(Active Record)的改变

一个改变是大多数时候忽略了对 tobelongs_to 的更新。当你创建一个 Student 类是 belongs_to 的关联类,创建一个 student 没有相关联的类是可行的。这导致了大量数据的不一致。对 Rails 5 来说,它的父类已经变成强制性的了。如果你在这里尝试插入一个空的记录,活动记录(Active Record)将会抛出异常。

改变对控制器(Controller)的测试

如果你测试你的模板并将其渲染到你的控制器,那你就做错了。对你的模板做个简单的变更将会留给你一个不眠之夜,抓住那个罪魁祸首。这就是为什么在 Rails 5 中,assert_template 被废弃的原因。但是,在目前你还会继续使用 assert_select 去检查特定的 DOM 元素。

如果你已经在一个控制方法内测试了实例变量,你就应该记得 assigns 也应该消失了。


If you’re writing controller tests, the new decree is that you should be concerned with cookies, HTTP return code, and DB calls, if any. Wait, that sounds just like an integration test. Well, you got me there…controller tests will likely be dropped in favor of integration tests in the future.

Note:* Checkout Rails Dom testing for more details and best practices.

Wrapping Up

With javascript playing a dominant role in web development today, web frameworks are getting reduced to mere API services. The Rails core team realizes this and is moving in the right direction. Feel free to join us in the comments section for a discussion.

如果你正在写控制器的测试用例,需要关注 cookies 的新命令,HTTP 返回值和 DB 调用。如果有得话,等等,这听起来像一个集合测试。好吧,你知道我想表达什么了。。。在将来的集成测试中会去掉控制器测试。

注意:点击链Rails Dom testing取等多详细内容和最佳实践

专注

在 web 开发中 javascript 扮演着统治地位的今天,web 框架仅仅是在降低更少的 api 服务。Rails 核心团队意识到这点并且在朝向正确的方向。欢迎在评论中加入我们的讨论行列。

返回顶部
顶部