Responses
An action responds by calling one render method — usually as the last expression in call.
JSON
render_json({id: post.id, title: post.title})
render_json(posts.map { |post| {id: post.id, title: post.title} })
render_json({error: "conflict"}, status_code: 409)Anything with a to_json method works: named tuples, hashes, arrays, and your own types that include JSON::Serializable. The Content-Type is application/json.
For larger or reused JSON shapes, use a Jbuilder template instead.
Plain text
render_plain "pong"
render_plain "Not here", status_code: 404Sets Content-Type: text/plain.
A body with any content type
render_body csv_string, content_type: "text/csv"
render_body "<h1>Hello</h1>", content_type: "text/html", status_code: 200content_type: is optional; without it, no Content-Type header is set.
Files
render_file "storage/reports/2026-q3.pdf",
content_type: "application/pdf",
filename: "report.pdf" # suggested download name| Option | Default | Description |
|---|---|---|
content_type: | required | The file's MIME type |
filename: | nil | Download name in Content-Disposition |
disposition: | "attachment" | Use "inline" to display in the browser |
status_code: | 200 |
The path is resolved relative to the working directory. Never pass a path built from user input without checking it. To serve a whole directory of public assets, use Static Files instead.
Redirects
redirect "/posts" # 302 Found
redirect "/posts/#{post.id}", status_code: 303 # See Other, after a POST
redirect "https://example.com", status_code: 301Empty responses
render_empty sends no body. Use it for 204 No Content, 304 Not Modified and HEAD responses:
class Posts::DeletePost < Blog::BaseAction
delete "/posts/:id"
call do
input = validate(PostPathContract)
if store.delete_post(id: input.id) > 0 # rows deleted
render_empty status_code: 204
else
render_json({error: "post_not_found"}, status_code: 404)
end
end
endRuncobo never strips a body for you based on the method or status code — you decide. A route that answers both GET and HEAD:
require "digest/sha1"
class Posts::ExportPost < Blog::BaseAction
get "/posts/:id/export"
head "/posts/:id/export"
call do
input = validate(PostPathContract)
if post = store.get_post?(id: input.id)
body = "#{post.title}\n"
etag = %("#{Digest::SHA1.hexdigest(body)}")
context.response.headers["ETag"] = etag
if request.method == "HEAD"
context.response.content_length = body.bytesize
render_empty content_type: "text/plain"
elsif request.headers["If-None-Match"]? == etag
render_empty status_code: 304
else
render_body body, content_type: "text/plain"
end
else
render_empty status_code: 404
end
end
endTemplates
render renders a Jbuilder or Water template chosen by naming convention. See Views.
Headers, cookies and status
Use context.response for anything the render methods don't cover. Set headers before rendering — once the body starts, headers are sent:
call do
context.response.headers["Cache-Control"] = "private, max-age=60"
context.response.cookies << HTTP::Cookie.new("theme", "dark", http_only: true)
render_json({ok: true})
endSummary
| Method | Content-Type | Default status |
|---|---|---|
render_json(value) | application/json | 200 |
render_plain(text) | text/plain | 200 |
render_body(body, content_type: nil) | as given | 200 |
render_file(path, content_type:) | as given | 200 |
render_empty(content_type: nil) | as given | 200 |
redirect(url) | — | 302 |
render | from the template engine | 200 |
All of them accept status_code:.