Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
d2e7c1b9
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,体验更适合开发者的 AI 搜索 >>
提交
d2e7c1b9
编写于
1月 10, 2010
作者:
J
José Valim
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Raise an error if respond_with is invoked and no format is declared.
上级
8d72ba51
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
68 addition
and
74 deletion
+68
-74
actionpack/lib/action_controller/metal/mime_responds.rb
actionpack/lib/action_controller/metal/mime_responds.rb
+6
-3
actionpack/test/controller/mime_responds_test.rb
actionpack/test/controller/mime_responds_test.rb
+62
-68
actionpack/test/fixtures/respond_with/using_defaults.js.rjs
actionpack/test/fixtures/respond_with/using_defaults.js.rjs
+0
-1
actionpack/test/fixtures/respond_with/using_defaults_with_type_list.js.rjs
...ixtures/respond_with/using_defaults_with_type_list.js.rjs
+0
-1
actionpack/test/fixtures/respond_with/using_defaults_with_type_list.xml.builder
...es/respond_with/using_defaults_with_type_list.xml.builder
+0
-1
actionpack/test/fixtures/respond_with/using_resource_with_block.html.erb
.../fixtures/respond_with/using_resource_with_block.html.erb
+0
-0
未找到文件。
actionpack/lib/action_controller/metal/mime_responds.rb
浏览文件 @
d2e7c1b9
...
...
@@ -215,7 +215,10 @@ def respond_to(*mimes, &block)
# a proc to it.
#
def
respond_with
(
*
resources
,
&
block
)
if
response
=
retrieve_response_from_mimes
([],
&
block
)
raise
"In order to use respond_with, first you need to declare the formats your "
<<
"controller responds to in the class level"
if
mimes_for_respond_to
.
empty?
if
response
=
retrieve_response_from_mimes
(
&
block
)
options
=
resources
.
extract_options!
options
.
merge!
(
:default_response
=>
response
)
(
options
.
delete
(
:responder
)
||
responder
).
call
(
self
,
resources
,
options
)
...
...
@@ -246,9 +249,9 @@ def collect_mimes_from_class_level #:nodoc:
# Collects mimes and return the response for the negotiated format. Returns
# nil if :not_acceptable was sent to the client.
#
def
retrieve_response_from_mimes
(
mimes
,
&
block
)
def
retrieve_response_from_mimes
(
mimes
=
nil
,
&
block
)
collector
=
Collector
.
new
{
default_render
}
mimes
=
collect_mimes_from_class_level
if
mimes
.
empty?
mimes
||=
collect_mimes_from_class_level
mimes
.
each
{
|
mime
|
collector
.
send
(
mime
)
}
block
.
call
(
collector
)
if
block_given?
...
...
actionpack/test/controller/mime_responds_test.rb
浏览文件 @
d2e7c1b9
...
...
@@ -461,31 +461,27 @@ def test_format_with_custom_response_type_and_request_headers
class
RespondWithController
<
ActionController
::
Base
respond_to
:html
,
:json
respond_to
:xml
,
:except
=>
:using_
defaults
respond_to
:js
,
:only
=>
[
:using_
defaults
,
:using_resource
]
respond_to
:xml
,
:except
=>
:using_
resource_with_block
respond_to
:js
,
:only
=>
[
:using_
resource_with_block
,
:using_resource
]
def
using_defaults
respond_to
do
|
format
|
format
.
csv
{
render
:text
=>
"CSV"
}
end
def
using_resource
respond_with
(
resource
)
end
def
using_defaults_with_type_list
respond_to
(
:js
,
:xml
)
def
using_resource_with_block
respond_with
(
resource
)
do
|
format
|
format
.
csv
{
render
:text
=>
"CSV"
}
end
end
def
default_overwritten
respond_
to
do
|
format
|
def
using_resource_with_overwrite_block
respond_
with
(
resource
)
do
|
format
|
format
.
html
{
render
:text
=>
"HTML"
}
end
end
def
using_resource
respond_with
(
Customer
.
new
(
"david"
,
13
))
end
def
using_resource_with_collection
respond_with
([
Customer
.
new
(
"david"
,
13
)
,
Customer
.
new
(
"jamis"
,
9
)])
respond_with
([
resource
,
Customer
.
new
(
"jamis"
,
9
)])
end
def
using_resource_with_parent
...
...
@@ -493,16 +489,16 @@ def using_resource_with_parent
end
def
using_resource_with_status_and_location
respond_with
(
Customer
.
new
(
"david"
,
13
)
,
:location
=>
"http://test.host/"
,
:status
=>
:created
)
respond_with
(
resource
,
:location
=>
"http://test.host/"
,
:status
=>
:created
)
end
def
using_resource_with_responder
responder
=
proc
{
|
c
,
r
,
o
|
c
.
render
:text
=>
"Resource name is
#{
r
.
first
.
name
}
"
}
respond_with
(
Customer
.
new
(
"david"
,
13
)
,
:responder
=>
responder
)
respond_with
(
resource
,
:responder
=>
responder
)
end
def
using_resource_with_action
respond_with
(
Customer
.
new
(
"david"
,
13
)
,
:action
=>
:foo
)
do
|
format
|
respond_with
(
resource
,
:action
=>
:foo
)
do
|
format
|
format
.
html
{
raise
ActionView
::
MissingTemplate
.
new
([],
"method"
)
}
end
end
...
...
@@ -511,11 +507,15 @@ def using_responder_with_respond
responder
=
Class
.
new
(
ActionController
::
Responder
)
do
def
respond
;
@controller
.
render
:text
=>
"respond
#{
format
}
"
;
end
end
respond_with
(
Customer
.
new
(
"david"
,
13
)
,
:responder
=>
responder
)
respond_with
(
resource
,
:responder
=>
responder
)
end
protected
def
resource
Customer
.
new
(
"david"
,
13
)
end
def
_render_js
(
js
,
options
)
self
.
content_type
||=
Mime
::
JS
self
.
response_body
=
js
.
respond_to?
(
:to_js
)
?
js
.
to_js
:
js
...
...
@@ -527,12 +527,18 @@ class InheritedRespondWithController < RespondWithController
respond_to
:xml
,
:json
def
index
respond_with
(
Customer
.
new
(
"david"
,
13
)
)
do
|
format
|
respond_with
(
resource
)
do
|
format
|
format
.
json
{
render
:text
=>
"JSON"
}
end
end
end
class
EmptyRespondWithController
<
ActionController
::
Base
def
index
respond_with
(
Customer
.
new
(
"david"
,
13
))
end
end
class
RespondWithControllerTest
<
ActionController
::
TestCase
tests
RespondWithController
...
...
@@ -547,56 +553,54 @@ def teardown
ActionController
::
Base
.
use_accept_header
=
false
end
def
test_using_defaults
def
test_using_resource
@request
.
accept
=
"text/javascript"
get
:using_resource
assert_equal
"text/javascript"
,
@response
.
content_type
assert_equal
'$("body").visualEffect("highlight");'
,
@response
.
body
@request
.
accept
=
"application/xml"
get
:using_resource
assert_equal
"application/xml"
,
@response
.
content_type
assert_equal
"<name>david</name>"
,
@response
.
body
@request
.
accept
=
"application/json"
assert_raise
ActionView
::
MissingTemplate
do
get
:using_resource
end
end
def
test_using_resource_with_block
@request
.
accept
=
"*/*"
get
:using_
defaults
get
:using_
resource_with_block
assert_equal
"text/html"
,
@response
.
content_type
assert_equal
'Hello world!'
,
@response
.
body
@request
.
accept
=
"text/csv"
get
:using_
defaults
get
:using_
resource_with_block
assert_equal
"text/csv"
,
@response
.
content_type
assert_equal
"CSV"
,
@response
.
body
@request
.
accept
=
"text/javascript"
get
:using_defaults
assert_equal
"text/javascript"
,
@response
.
content_type
assert_equal
'$("body").visualEffect("highlight");'
,
@response
.
body
end
def
test_using_defaults_with_type_list
@request
.
accept
=
"*/*"
get
:using_defaults_with_type_list
assert_equal
"text/javascript"
,
@response
.
content_type
assert_equal
'$("body").visualEffect("highlight");'
,
@response
.
body
@request
.
accept
=
"application/xml"
get
:using_
defaults_with_type_list
get
:using_
resource
assert_equal
"application/xml"
,
@response
.
content_type
assert_equal
"<
p>Hello world!</p>
\n
"
,
@response
.
body
assert_equal
"<
name>david</name>
"
,
@response
.
body
end
def
test_
default_overwritten
get
:
default_overwritten
def
test_
using_resource_with_overwrite_block
get
:
using_resource_with_overwrite_block
assert_equal
"text/html"
,
@response
.
content_type
assert_equal
"HTML"
,
@response
.
body
end
def
test_using_resource
@request
.
accept
=
"text/javascript"
get
:using_resource
assert_equal
"text/javascript"
,
@response
.
content_type
assert_equal
'$("body").visualEffect("highlight");'
,
@response
.
body
def
test_not_acceptable
@request
.
accept
=
"application/xml"
get
:using_resource
assert_equal
"application/xml"
,
@response
.
content_type
assert_equal
"<name>david</name>"
,
@response
.
body
get
:using_resource_with_block
assert_equal
406
,
@response
.
status
@request
.
accept
=
"application/json"
assert_raise
ActionView
::
MissingTemplate
do
get
:using_resource
end
@request
.
accept
=
"text/javascript"
get
:using_resource_with_overwrite_block
assert_equal
406
,
@response
.
status
end
def
test_using_resource_for_post_with_html_redirects_on_success
...
...
@@ -831,22 +835,12 @@ def test_using_resource_with_set_responder
RespondWithController
.
responder
=
ActionController
::
Responder
end
def
test_not_acceptable
@request
.
accept
=
"application/xml"
get
:using_defaults
assert_equal
406
,
@response
.
status
@request
.
accept
=
"text/html"
get
:using_defaults_with_type_list
assert_equal
406
,
@response
.
status
@request
.
accept
=
"application/json"
get
:using_defaults_with_type_list
assert_equal
406
,
@response
.
status
@request
.
accept
=
"text/javascript"
get
:default_overwritten
assert_equal
406
,
@response
.
status
def
test_error_is_raised_if_no_respond_to_is_declared_and_respond_with_is_called
@controller
=
EmptyRespondWithController
.
new
@request
.
accept
=
"*/*"
assert_raise
RuntimeError
do
get
:index
end
end
private
...
...
actionpack/test/fixtures/respond_with/using_defaults.js.rjs
已删除
100644 → 0
浏览文件 @
8d72ba51
page[:body].visual_effect :highlight
\ No newline at end of file
actionpack/test/fixtures/respond_with/using_defaults_with_type_list.js.rjs
已删除
100644 → 0
浏览文件 @
8d72ba51
page[:body].visual_effect :highlight
\ No newline at end of file
actionpack/test/fixtures/respond_with/using_defaults_with_type_list.xml.builder
已删除
100644 → 0
浏览文件 @
8d72ba51
xml.p "Hello world!"
\ No newline at end of file
actionpack/test/fixtures/respond_with/using_
defaults
.html.erb
→
actionpack/test/fixtures/respond_with/using_
resource_with_block
.html.erb
浏览文件 @
d2e7c1b9
文件已移动
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录