0
0

Railsのレスポンスに関連するメソッドまとめてみた

Posted at

Railsのレスポンス response について

Railsにおけるresponseオブジェクトの基本的な使い方や特性についてまとめています。

目次

逆引き

レスポンスの内容を文字列で取得

説明

レスポンスの内容を文字列で取得

使い方

response.body()

レスポンスのcharsetを取得

説明

レスポンスのcharsetを取得

使い方

response.charset()

レスポンスクッキーをハッシュに変換して取得

説明

レスポンスクッキーをハッシュに変換して取得

使い方

response.cookies()

# 例
assert_equal 'AuthorOfNewPage', r.cookies['author']

HTTPステータスコードに対応するメッセージを取得

説明

HTTPステータスコードに対応するメッセージを取得

使い方

response.message()

# 例
response.status = 200
response.message #=> "OK"
response.status = 404
response.message #=> "Not Found"

コンテントタイプを設定

説明

コンテントタイプを設定
HTTPレスポンスのコンテンツのMIMEタイプを設定

使い方

response.content_type = "コンテントタイプ"

# 例
response.content_type = "text/plain"

パスに格納されているファイルをレスポンスボディとして返す

説明

パスに格納されているファイルをレスポンスボディとして返す

使い方

response.send_file(パス)
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0