LoginSignup
1
2

More than 3 years have passed since last update.

redirect_toとrenderの違い

Posted at

はじめに

処理を書くたびにまだよく混同してしまうので、
パッとすぐ思い出せるようにメモしておきます。

違い

redirect_to

user_controller.rb
def ~
 # 処理

 redirect_to("/app/posts/new")
end

指定先:
URL(/URL/〜/〜/)

ルート:
controller → URL → route → controller → view

主な用途:
データの更新や削除が必要な場合など → 「適切なcontrollerでの処理が必要」なとき

render

user_controller.rb
def ~
 # 処理

 render("user/edit")
end

指定先:
ディレクトリ/ビュー

ルート:
controller → view

主な用途:
ログインや入力形式に失敗した場合など → 「ただエラーを表示させたい」とき

参考

【Rails】renderとredirect_toの違いと使い分け
【Rails】flashメッセージを使用して簡易メッセージを表示させる詳しい方法と解説

1
2
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
1
2