Skip to content

Commit d71a7b0

Browse files
committed
Add “return_no_content” helper to explicitly respond with a 204 status code and an empty body
1 parent 81cf0fb commit d71a7b0

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

lib/grape/dsl/inside_route.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,20 @@ def body(value = nil)
185185
end
186186
end
187187

188+
# Allows you to explicitly return no content.
189+
#
190+
# @example
191+
# delete :id do
192+
# return_no_content
193+
# "not returned"
194+
# end
195+
#
196+
# DELETE /12 # => 204 No Content, ""
197+
def return_no_content
198+
status 204
199+
body false
200+
end
201+
188202
# Allows you to define the response as a file-like object.
189203
#
190204
# @example

spec/grape/dsl/inside_route_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,14 @@ def initialize
143143
end
144144
end
145145

146+
describe '#return_no_content' do
147+
it 'sets the status code and body' do
148+
subject.return_no_content
149+
expect(subject.status).to eq 204
150+
expect(subject.body).to eq ''
151+
end
152+
end
153+
146154
describe '#content_type' do
147155
describe 'set' do
148156
before do

0 commit comments

Comments
 (0)