Skip to content

…​ -> ... #99

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion source/zh-CN/action_controller_overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ NOTE: “[”和“]”这两个符号不允许出现在 URL 中,所以上面

此时,`params[:ids]` 的值是 `["1", "2", "3"]`。注意,参数的值始终是字符串,Rails 不会尝试转换类型。

NOTE: 默认情况下,基于安全考虑,参数中的 `[nil]` 和 `[nil, nil, …​]` 会替换成 `[]`。详情参见 [生成不安全的查询](security.html#unsafe-query-generation)。
NOTE: 默认情况下,基于安全考虑,参数中的 `[nil]` 和 `[nil, nil, ...]` 会替换成 `[]`。详情参见 [生成不安全的查询](security.html#unsafe-query-generation)。

若想发送一个散列,要在方括号内指定键名:

Expand Down
4 changes: 2 additions & 2 deletions source/zh-CN/api_documentation_guidelines.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ RDoc 的[标记](http://docs.seattlerb.org/rdoc/RDoc/Markup.html)和[额外的

使用简单的陈述句。简短更好,要说到点子上。

使用现在时:“Returns a hash that…​”,而非“Returned a hash that…​”或“Will return a hash that…​”。
使用现在时:“Returns a hash that...”,而非“Returned a hash that...”或“Will return a hash that...”。

注释的第一个字母大写,后续内容遵守常规的标点符号规则:

Expand Down Expand Up @@ -222,7 +222,7 @@ class Array
end
```

WARNING: 只有简单的内容才能使用 `+...+` 标记使用等宽字体,如常规的方法名、符号、路径(含有正斜线),等等。其他内容应该使用 `<tt>&#8230;&#8203;</tt>`,尤其是带有命名空间的类名或模块名,如 `<tt>ActiveRecord::Base</tt>`。
WARNING: 只有简单的内容才能使用 `+...+` 标记使用等宽字体,如常规的方法名、符号、路径(含有正斜线),等等。其他内容应该使用 `<tt>...</tt>`,尤其是带有命名空间的类名或模块名,如 `<tt>ActiveRecord::Base</tt>`。


可以使用下述命令测试 RDoc 的输出:
Expand Down
2 changes: 1 addition & 1 deletion source/zh-CN/asset_pipeline.md
Original file line number Diff line number Diff line change
Expand Up @@ -814,7 +814,7 @@ config.assets.css_compressor = :sass

压缩 JavaScript 的可选方式有 `:closure`、`:uglifier` 和 `:yui`,分别要求在 Gemfile 中添加 `closure-compiler`、`uglifier` 和 `yui-compressor` gem。

默认情况下,Gemfile 中包含了 [uglifier](https://github.com/lautis/uglifier) gem,这个 gem 使用 Ruby 包装 [UglifyJS](https://github.com/mishoo/UglifyJS)(使用 NodeJS 开发),作用是通过删除空白和注释、缩短局部变量名及其他微小优化(例如在可能的情况下把 `if&#8230;&#8203;else` 语句修改为三元运算符)压缩 JavaScript 代码。
默认情况下,Gemfile 中包含了 [uglifier](https://github.com/lautis/uglifier) gem,这个 gem 使用 Ruby 包装 [UglifyJS](https://github.com/mishoo/UglifyJS)(使用 NodeJS 开发),作用是通过删除空白和注释、缩短局部变量名及其他微小优化(例如在可能的情况下把 `if...else` 语句修改为三元运算符)压缩 JavaScript 代码。

使用 `uglifier` 压缩 JavaScript 需进行如下设置:

Expand Down
52 changes: 26 additions & 26 deletions source/zh-CN/association_basics.md
Original file line number Diff line number Diff line change
Expand Up @@ -1552,19 +1552,19 @@ end
声明 `has_many` 关联后,声明所在的类自动获得了 16 个关联相关的方法:

* `collection`
* `collection<<(object, &#8230;&#8203;)`
* `collection.delete(object, &#8230;&#8203;)`
* `collection.destroy(object, &#8230;&#8203;)`
* `collection<<(object, ...)`
* `collection.delete(object, ...)`
* `collection.destroy(object, ...)`
* `collection=(objects)`
* `collection_singular_ids`
* `collection_singular_ids=(ids)`
* `collection.clear`
* `collection.empty?`
* `collection.size`
* `collection.find(&#8230;&#8203;)`
* `collection.where(&#8230;&#8203;)`
* `collection.exists?(&#8230;&#8203;)`
* `collection.build(attributes = {}, &#8230;&#8203;)`
* `collection.find(...)`
* `collection.where(...)`
* `collection.exists?(...)`
* `collection.build(attributes = {}, ...)`
* `collection.create(attributes = {})`
* `collection.create!(attributes = {})`

Expand Down Expand Up @@ -1609,7 +1609,7 @@ books.create!(attributes = {})

<a class="anchor" id="methods-added-by-has-many-collection-object"></a>

##### `collection<<(object, &#8230;&#8203;)`
##### `collection<<(object, ...)`

`collection<<` 方法向关联对象数组中添加一个或多个对象,并把各个所加对象的外键设为调用此方法的模型的主键。

Expand All @@ -1619,7 +1619,7 @@ books.create!(attributes = {})

<a class="anchor" id="methods-added-by-has-many-collection-delete-object"></a>

##### `collection.delete(object, &#8230;&#8203;)`
##### `collection.delete(object, ...)`

`collection.delete` 方法从关联对象数组中删除一个或多个对象,并把删除的对象外键设为 `NULL`。

Expand All @@ -1631,7 +1631,7 @@ WARNING: 如果关联设置了 `dependent: :destroy`,还会销毁关联的对

<a class="anchor" id="methods-added-by-has-many-collection-destroy-object"></a>

##### `collection.destroy(object, &#8230;&#8203;)`
##### `collection.destroy(object, ...)`

`collection.destroy` 方法在关联对象上调用 `destroy` 方法,从关联对象数组中删除一个或多个对象。

Expand Down Expand Up @@ -1699,7 +1699,7 @@ WARNING: 如果设为 `dependent: :destroy`,对象会被删除,这与 `depen

<a class="anchor" id="methods-added-by-has-many-collection-find"></a>

##### `collection.find(&#8230;&#8203;)`
##### `collection.find(...)`

`collection.find` 方法在集合中查找对象,使用的句法和选项跟 `ActiveRecord::Base.find` 方法一样。

Expand All @@ -1709,7 +1709,7 @@ WARNING: 如果设为 `dependent: :destroy`,对象会被删除,这与 `depen

<a class="anchor" id="methods-added-by-has-many-collection-where"></a>

#### `collection.where(&#8230;&#8203;)`
#### `collection.where(...)`

`collection.where` 方法根据指定的条件在集合中查找对象,但对象是惰性加载的,即访问对象时才会查询数据库。

Expand All @@ -1720,13 +1720,13 @@ WARNING: 如果设为 `dependent: :destroy`,对象会被删除,这与 `depen

<a class="anchor" id="methods-added-by-has-many-collection-exists-questionmark"></a>

##### `collection.exists?(&#8230;&#8203;)`
##### `collection.exists?(...)`

`collection.exists?` 方法根据指定的条件检查集合中是否有符合条件的对象,使用的句法和选项跟 [`ActiveRecord::Base.exists?` 方法](http://api.rubyonrails.org/classes/ActiveRecord/FinderMethods.html#method-i-exists-3F)一样。

<a class="anchor" id="methods-added-by-has-many-collection-build-attributes"></a>

##### `collection.build(attributes = {}, &#8230;&#8203;)`
##### `collection.build(attributes = {}, ...)`

`collection.build` 方法返回一个或多个此种关联类型的新对象。这些对象会使用传入的属性初始化,还会创建对应的外键,但不会保存关联的对象。

Expand Down Expand Up @@ -2139,18 +2139,18 @@ person.articles << article unless person.articles.include?(article)
声明 `has_and_belongs_to_many` 关联后,声明所在的类自动获得了 16 个关联相关的方法:

* `collection`
* `collection<<(object, &#8230;&#8203;)`
* `collection.delete(object, &#8230;&#8203;)`
* `collection.destroy(object, &#8230;&#8203;)`
* `collection<<(object, ...)`
* `collection.delete(object, ...)`
* `collection.destroy(object, ...)`
* `collection=(objects)`
* `collection_singular_ids`
* `collection_singular_ids=(ids)`
* `collection.clear`
* `collection.empty?`
* `collection.size`
* `collection.find(&#8230;&#8203;)`
* `collection.where(&#8230;&#8203;)`
* `collection.exists?(&#8230;&#8203;)`
* `collection.find(...)`
* `collection.where(...)`
* `collection.exists?(...)`
* `collection.build(attributes = {})`
* `collection.create(attributes = {})`
* `collection.create!(attributes = {})`
Expand Down Expand Up @@ -2204,7 +2204,7 @@ WARNING: 在 `has_and_belongs_to_many` 关联的联结表中使用其他字段

<a class="anchor" id="methods-added-by-has-and-belongs-to-many-collection-object"></a>

##### `collection<<(object, &#8230;&#8203;)`
##### `collection<<(object, ...)`

`collection<<` 方法向集合中添加一个或多个对象,并在联结表中创建相应的记录。

Expand All @@ -2216,7 +2216,7 @@ NOTE: 这个方法是 `collection.concat` 和 `collection.push` 的别名。

<a class="anchor" id="methods-added-by-has-and-belongs-to-many-collection-delete-object"></a>

##### `collection.delete(object, &#8230;&#8203;)`
##### `collection.delete(object, ...)`

`collection.delete` 方法从集合中删除一个或多个对象,并删除联结表中相应的记录,但是不会销毁对象。

Expand All @@ -2226,7 +2226,7 @@ NOTE: 这个方法是 `collection.concat` 和 `collection.push` 的别名。

<a class="anchor" id="methods-added-by-has-and-belongs-to-many-collection-destroy-object"></a>

##### `collection.destroy(object, &#8230;&#8203;)`
##### `collection.destroy(object, ...)`

`collection.destroy` 方法把集合中指定对象在联结表中的记录删除。这个方法不会销毁对象本身。

Expand Down Expand Up @@ -2286,7 +2286,7 @@ NOTE: 这个方法是 `collection.concat` 和 `collection.push` 的别名。

<a class="anchor" id="methods-added-by-has-and-belongs-to-many-collection-find"></a>

##### `collection.find(&#8230;&#8203;)`
##### `collection.find(...)`

`collection.find` 方法在集合中查找对象,使用的句法和选项跟 `ActiveRecord::Base.find` 方法一样。此外还限制对象必须在集合中。

Expand All @@ -2296,7 +2296,7 @@ NOTE: 这个方法是 `collection.concat` 和 `collection.push` 的别名。

<a class="anchor" id="methods-added-by-has-and-belongs-to-many-collection-where"></a>

##### `collection.where(&#8230;&#8203;)`
##### `collection.where(...)`

`collection.where` 方法根据指定的条件在集合中查找对象,但对象是惰性加载的,访问对象时才执行查询。此外还限制对象必须在集合中。

Expand All @@ -2306,7 +2306,7 @@ NOTE: 这个方法是 `collection.concat` 和 `collection.push` 的别名。

<a class="anchor" id="methods-added-by-has-and-belongs-to-many-collection-exists-questionmark"></a>

##### `collection.exists?(&#8230;&#8203;)`
##### `collection.exists?(...)`

`collection.exists?` 方法根据指定的条件检查集合中是否有符合条件的对象,使用的句法和选项跟 `ActiveRecord::Base.exists?` 方法一样。

Expand Down
2 changes: 1 addition & 1 deletion source/zh-CN/configuring.md
Original file line number Diff line number Diff line change
Expand Up @@ -1170,4 +1170,4 @@ end

在 Linux 和 macOS 中无需额外的 gem,[*BSD](https://github.com/guard/listen#on-bsd) 和 [Windows](https://github.com/guard/listen#on-windows) 可能需要。

注意,[某些设置不支持](https://github.com/guard/listen#issues&#8212;&#8203;limitations)。
注意,[某些设置不支持](https://github.com/guard/listen#issues--limitations)。
4 changes: 2 additions & 2 deletions source/zh-CN/debugging_rails_applications.md
Original file line number Diff line number Diff line change
Expand Up @@ -698,8 +698,8 @@ No breakpoints.

断点也可以启用或禁用:

* `enable breakpoints [n [m [&#8230;&#8203;]]]`:在指定的断点列表或者所有断点处停止应用。这是创建断点后的默认状态。
* `disable breakpoints [n [m [&#8230;&#8203;]]]`:让指定的断点(或全部断点)在应用中不起作用。
* `enable breakpoints [n [m [...]]]`:在指定的断点列表或者所有断点处停止应用。这是创建断点后的默认状态。
* `disable breakpoints [n [m [...]]]`:让指定的断点(或全部断点)在应用中不起作用。

<a class="anchor" id="catching-exceptions"></a>

Expand Down
8 changes: 4 additions & 4 deletions source/zh-CN/security.md
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ NOTE: 对于净化、保护和验证操作,白名单优于黑名单。

黑名单可以包含垃圾电子邮件地址、非公开的控制器动作、造成安全威胁的 HTML 标签等等。与此相反,白名单可以包含可靠的电子邮件地址、公开的控制器动作、安全的 HTML 标签等等。尽管有些情况下我们无法创建白名单(例如在垃圾信息过滤器中),但只要有可能就应该优先使用白名单:

* 对于安全相关的控制器动作,在 `before_action` 选项中用 `except: [&#8230;&#8203;]` 代替 `only: [&#8230;&#8203;]`,这样就不会忘记为新建动作启用安全检查;
* 对于安全相关的控制器动作,在 `before_action` 选项中用 `except: [...]` 代替 `only: [...]`,这样就不会忘记为新建动作启用安全检查;
* 为防止跨站脚本(XSS)攻击,应允许使用 `<strong>` 标签,而不是去掉 `<script>` 标签,详情请参阅后文;
* 不要尝试通过黑名单来修正用户输入:

Expand Down Expand Up @@ -714,7 +714,7 @@ SELECT * FROM projects WHERE (name = '') UNION

#### 对策

Ruby on Rails 内置了针对特殊 SQL 字符的过滤器,用于转义 `'`、`"`、`NULL` 和换行符。当我们使用 `Model.find(id)` 和 `Model.find_by_something(something)` 方法时,Rails 会自动应用这个过滤器。但在 SQL 片段中,尤其是在条件片段(`where("&#8230;&#8203;")`)中,需要为 `connection.execute()` 和 `Model.find_by_sql()` 方法手动应用这个过滤器。
Ruby on Rails 内置了针对特殊 SQL 字符的过滤器,用于转义 `'`、`"`、`NULL` 和换行符。当我们使用 `Model.find(id)` 和 `Model.find_by_something(something)` 方法时,Rails 会自动应用这个过滤器。但在 SQL 片段中,尤其是在条件片段(`where("...")`)中,需要为 `connection.execute()` 和 `Model.find_by_sql()` 方法手动应用这个过滤器。

为了净化受污染的字符串,在提供查询条件的选项时,我们应该传入数组而不是直接传入字符串:

Expand Down Expand Up @@ -1048,7 +1048,7 @@ unless params[:token].nil?
end
```

只要 `params[:token]` 的值是 `[nil]`、`[nil, nil, &#8230;&#8203;]` 和 `['foo', nil]` 其中之一,上述测试就会被被绕过,而带有 `IS NULL` 或 `IN ('foo', NULL)` 的 WHERE 子句仍将被添加到 SQL 查询中。
只要 `params[:token]` 的值是 `[nil]`、`[nil, nil, ...]` 和 `['foo', nil]` 其中之一,上述测试就会被被绕过,而带有 `IS NULL` 或 `IN ('foo', NULL)` 的 WHERE 子句仍将被添加到 SQL 查询中。

默认情况下,为了保证数据库安全,`deep_munge` 方法会把某些值替换为 `nil`。下述表格列出了经过替换处理后 JSON 请求和查询参数的对应关系:

Expand All @@ -1057,7 +1057,7 @@ end
| `{ "person": null }` | `{ :person => nil }` |
| `{ "person": [] }` | `{ :person => [ }]` |
| `{ "person": [null] }` | `{ :person => [ }]` |
| `{ "person": [null, null, &#8230;&#8203;] }` | `{ :person => [ }]` |
| `{ "person": [null, null, ...] }` | `{ :person => [ }]` |
| `{ "person": ["foo", null] }` | `{ :person => ["foo" }]` |

当然,如果我们非常了解这类安全风险并知道如何处理,也可以通过设置禁用 `deep_munge` 方法:
Expand Down
4 changes: 2 additions & 2 deletions source/zh-CN/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ end
| `assert_in_delta( expected, actual, [delta], [msg] )` | 确保 `expected` 和 `actual` 的差值在 `delta` 的范围内。 |
| `assert_not_in_delta( expected, actual, [delta], [msg] )` | 确保 `expected` 和 `actual` 的差值不在 `delta` 的范围内。 |
| `assert_throws( symbol, [msg] ) { block }` | 确保指定的块会抛出指定符号表示的异常。 |
| `assert_raises( exception1, exception2, &#8230;&#8203; ) { block }` | 确保指定块会抛出指定异常中的一个。 |
| `assert_raises( exception1, exception2, ... ) { block }` | 确保指定块会抛出指定异常中的一个。 |
| `assert_instance_of( class, obj, [msg] )` | 确保 `obj` 是 `class` 的实例。 |
| `assert_not_instance_of( class, obj, [msg] )` | 确保 `obj` 不是 `class` 的实例。 |
| `assert_kind_of( class, obj, [msg] )` | 确保 `obj` 是 `class` 或其后代的实例。 |
Expand Down Expand Up @@ -337,7 +337,7 @@ NOTE: 自己创建断言是高级话题,本文不涉及。

| 断言 | 作用 |
|---|---|
| `assert_difference(expressions, difference = 1, message = nil) {&#8230;&#8203;}` | 运行代码块前后数量变化了多少(通过 `expression` 表示)。 |
| `assert_difference(expressions, difference = 1, message = nil) {...}` | 运行代码块前后数量变化了多少(通过 `expression` 表示)。 |
| `assert_no_difference(expressions, message = nil, &block)` | 运行代码块前后数量没变多少(通过 `expression` 表示)。 |
| `assert_nothing_raised { block }` | 确保指定的块不会抛出任何异常。 |
| `assert_recognizes(expected_options, path, extras={}, message=nil)` | 断言正确处理了指定路径,而且解析的参数(通过 `expected_options` 散列指定)与路径匹配。基本上,它断言 Rails 能识别 `expected_options` 指定的路由。 |
Expand Down
16 changes: 8 additions & 8 deletions source/zh-CN/upgrading_ruby_on_rails.md
Original file line number Diff line number Diff line change
Expand Up @@ -1104,7 +1104,7 @@ Rails 4.0 不再支持从 `vendor/plugins` 目录中加载插件。插件应该
* 关联集合的 `delete` 方法的参数现在除了记录之外还可以使用 `Integer` 或 `String`,基本与 `destroy` 方法一样。以前,传入这样的参数时会抛出 `ActiveRecord::AssociationTypeMismatch` 异常。从 Rails 4.0 开始,`delete` 在删除记录之前会自动查找指定 ID 对应的记录。
* 在 Rails 4.0 中,如果修改了列或表的名称,相关的索引也会重命名。现在无需编写迁移重命名索引了。
* Rails 4.0 把 `serialized_attributes` 和 `attr_readonly` 改成只有类方法版本了。别再使用实例方法版本了,因为已经弃用。应该把实例方法版本改成类方法版本,例如把 `self.serialized_attributes` 改成 `self.class.serialized_attributes`。
* 使用默认的编码器时,把 `nil` 赋值给序列化的属性在数据库中保存的是 `NULL`,而不是通过 `YAML ("--- \n&#8230;&#8203;\n")` 传递 `nil` 值。
* 使用默认的编码器时,把 `nil` 赋值给序列化的属性在数据库中保存的是 `NULL`,而不是通过 `YAML ("--- \n...\n")` 传递 `nil` 值。
* Rails 4.0 删除了 `attr_accessible` 和 `attr_protected`,换成了健壮参数(strong parameter)。平滑升级可以使用 [`protected_attributes`](https://github.com/rails/protected_attributes) gem。
* 如果不使用 `protected_attributes` gem,可以把与它有关的选项都删除,例如 `whitelist_attributes` 或 `mass_assignment_sanitizer`。
* Rails 4.0 要求作用域使用可调用的对象,如 Proc 或 lambda:
Expand All @@ -1120,16 +1120,16 @@ Rails 4.0 不再支持从 `vendor/plugins` 目录中加载插件。插件应该
* Rails 4.0 弃用了 `ActiveRecord::Fixtures`,改成了 `ActiveRecord::FixtureSet`。
* Rails 4.0 弃用了 `ActiveRecord::TestCase`,改成了 `ActiveSupport::TestCase`。
* Rails 4.0 弃用了以前基于散列的查找方法 API。这意味着,不能再给查找方法传入选项了。例如,`Book.find(:all, conditions: { name: '1984' })` 已经弃用,改成了 `Book.where(name: '1984')`。
* 除了 `find_by_&#8230;&#8203;` 和 `find_by_&#8230;&#8203;!`,其他动态查找方法都弃用了。新旧变化如下:
* 除了 `find_by_...` 和 `find_by_...!`,其他动态查找方法都弃用了。新旧变化如下:

* `find_all_by_&#8230;&#8203;` 变成 `where(&#8230;&#8203;)`
* `find_last_by_&#8230;&#8203;` 变成 `where(&#8230;&#8203;).last`
* `scoped_by_&#8230;&#8203;` 变成 `where(&#8230;&#8203;)`
* `find_or_initialize_by_&#8230;&#8203;` 变成 `find_or_initialize_by(&#8230;&#8203;)`
* `find_or_create_by_&#8230;&#8203;` 变成 `find_or_create_by(&#8230;&#8203;)`
* `find_all_by_...` 变成 `where(...)`
* `find_last_by_...` 变成 `where(...).last`
* `scoped_by_...` 变成 `where(...)`
* `find_or_initialize_by_...` 变成 `find_or_initialize_by(...)`
* `find_or_create_by_...` 变成 `find_or_create_by(...)`


* 注意,`where(&#8230;&#8203;)` 返回一个关系,而不像旧的查找方法那样返回一个数组。如果需要使用数组,调用 `where(&#8230;&#8203;).to_a`。
* 注意,`where(...)` 返回一个关系,而不像旧的查找方法那样返回一个数组。如果需要使用数组,调用 `where(...).to_a`。
* 等价的方法所执行的 SQL 语句可能与以前的实现不同。
* 如果想使用旧的查找方法,可以使用 [`activerecord-deprecated_finders`](https://github.com/rails/activerecord-deprecated_finders) gem。
* Rails 4.0 修改了 `has_and_belongs_to_many` 关联默认的联结表名,把第二个表名中的相同前缀去掉。现有的 `has_and_belongs_to_many` 关联,如果表名中有共用的前缀,要使用 `join_table` 选项指定。例如:
Expand Down