Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
c942298f
R
rails
项目概览
张重言
/
rails
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rails
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
c942298f
编写于
1月 28, 2016
作者:
R
Rafael França
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #23323 from maclover7/api-app-docs
Docs review of api_app.md
上级
c082a7aa
3745d543
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
19 addition
and
16 deletion
+19
-16
guides/source/api_app.md
guides/source/api_app.md
+19
-16
未找到文件。
guides/source/api_app.md
浏览文件 @
c942298f
...
...
@@ -44,11 +44,11 @@ using Rails is: "isn't using Rails to spit out some JSON overkill? Shouldn't I
just use something like Sinatra?".
For very simple APIs, this may be true. However, even in very HTML-heavy
applications, most of an application's logic
is actually
outside of the view
applications, most of an application's logic
lives
outside of the view
layer.
The reason most people use Rails is that it provides a set of defaults that
allows
us to get up and running quickly
without having to make a lot of trivial
allows
developers to get up and running quickly,
without having to make a lot of trivial
decisions.
Let's take a look at some of the things that Rails provides out of the box that are
...
...
@@ -97,7 +97,7 @@ Handled at the Action Pack layer:
means not having to spend time thinking about how to model your API in terms
of HTTP.
-
URL Generation: The flip side of routing is URL generation. A good API based
on HTTP includes URLs (see
[
the GitHub
g
ist API
](
http://developer.github.com/v3/gists/
)
on HTTP includes URLs (see
[
the GitHub
G
ist API
](
http://developer.github.com/v3/gists/
)
for an example).
-
Header and Redirection Responses:
`head :no_content`
and
`redirect_to user_url(current_user)`
come in handy. Sure, you could manually
...
...
@@ -126,7 +126,7 @@ when configuring Active Record.
**The short version is**
: you may not have thought about which parts of Rails
are still applicable even if you remove the view layer, but the answer turns out
to be
"most of it"
.
to be
most of it
.
The Basic Configuration
-----------------------
...
...
@@ -192,10 +192,11 @@ An API application comes with the following middleware by default:
-
`Rack::Sendfile`
-
`ActionDispatch::Static`
-
`ActionDispatch::LoadInterlock`
-
`ActiveSupport::Cache::Strategy::LocalCache::Middleware`
-
`Rack::Runtime`
-
`ActionDispatch::RequestId`
-
`Rails::Rack::Logger`
-
`Rack::Runtime`
-
`ActionDispatch::ShowExceptions`
-
`ActionDispatch::DebugExceptions`
-
`ActionDispatch::RemoteIp`
...
...
@@ -323,7 +324,7 @@ will be:
{
:person
=>
{
:firstName
=>
"Yehuda"
,
:lastName
=>
"Katz"
}
}
```
### Other Middleware
s
### Other Middleware
Rails ships with a number of other middleware that you might want to use in an
API application, especially if one of your API clients is the browser:
...
...
@@ -360,22 +361,24 @@ Choosing Controller Modules
An API application (using
`ActionController::API`
) comes with the following
controller modules by default:
-
`ActionController::UrlFor`
: Makes
`url_for`
and
friend
s available.
-
`ActionController::UrlFor`
: Makes
`url_for`
and
similar helper
s available.
-
`ActionController::Redirecting`
: Support for
`redirect_to`
.
-
`A
ctionController::
Rendering`
: Basic support for rendering.
-
`A
bstractController::Rendering`
and
`ActionController::Api
Rendering`
: Basic support for rendering.
-
`ActionController::Renderers::All`
: Support for
`render :json`
and friends.
-
`ActionController::ConditionalGet`
: Support for
`stale?`
.
-
`ActionController::ForceSSL`
: Support for
`force_ssl`
.
-
`ActionController::DataStreaming`
: Support for
`send_file`
and
`send_data`
.
-
`AbstractController::Callbacks`
: Support for
`before_action`
and friends.
-
`ActionController::Instrumentation`
: Support for the instrumentation
hooks defined by Action Controller (see
[
the instrumentation
guide
](
active_support_instrumentation.html#action-controller
)
).
-
`ActionController::Rescue`
: Support for
`rescue_from`
.
-
`ActionController::BasicImplicitRender`
: Makes sure to return an empty response
if there's not an explicit one.
-
`ActionController::StrongParameters`
: Support for parameters white-listing in
combination with Active Model mass assignment.
-
`ActionController::ForceSSL`
: Support for
`force_ssl`
.
-
`ActionController::DataStreaming`
: Support for
`send_file`
and
`send_data`
.
-
`AbstractController::Callbacks`
: Support for
`before_action`
and
similar helpers.
-
`ActionController::Rescue`
: Support for
`rescue_from`
.
-
`ActionController::Instrumentation`
: Support for the instrumentation
hooks defined by Action Controller (see
[
the instrumentation
guide
](
active_support_instrumentation.html#action-controller
)
for
more information regarding this).
-
`ActionController::ParamsWrapper`
: Wraps the parameters hash into a nested hash
so you don't have to specify root elements sending POST requests for instance.
...
...
@@ -404,5 +407,5 @@ Some common modules you might want to add:
-
`ActionController::Cookies`
: Support for
`cookies`
, which includes
support for signed and encrypted cookies. This requires the cookies middleware.
The best place to add a module is in your
`ApplicationController`
but you can
The best place to add a module is in your
`ApplicationController`
,
but you can
also add modules to individual controllers.
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录