LoginSignup
0
0

More than 3 years have passed since last update.

redirect_toとrenderの違い

Posted at

redirect_toの場合

redirect_to=>ルーティング=>コントローラー=>ビュー
新たなリクエストがされたのと同じ動きになり、コントローラーを経由してビューが表示されます。

renderの場合

render=>ビュー
そのままビューが表示されます。

redirect_toとrenderの違い

いずれも実行するとビューが表示されます。
しかしこれによって、元のインスタンス変数の値が上書きされるかどうかが違います。
【例】例えば@teamという変数があるとして、@teamの変数にはエラーメッセージが代入されているため、これが上書きされないようrenderによってビューを表示させる必要があります。

def create
  @team = Team.new(team_params)
  if @team.save
    redirect_to root_path, notice: 'チームを作成しました'
  else
    render :new
  end
end
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