AR삽질러

Rails 一日一つメソッド (Day14) redirect_toメソッド 본문

Ruby/RubyOnRails-日本語

Rails 一日一つメソッド (Day14) redirect_toメソッド

아랑팡팡 2024. 1. 3. 14:32
728x90

 

Rails 一日一つメソッド (Day14) redirect_toメソッド

 

redirect_toメソッドとは

 - 指定したURLに遷移させることがでくるメソッド。

 - ユーザーのブラウザを他のURLにリダイレクトさせる。

redirect_to "リダイレクト先”

 

railsではパスが送られるとルーチィングで決められたCotrollerのアクションが実行される。

redirect_toメソッドを使うと、決められたCotrollerのアクション以外のアクションなどを実行させ、選択したビューフィイルを表紙する。

 

redirectメソッドの使い方

 1) URLで指定

指定したURLにredirectする。

redirect_to "URL"
redirect_to "http://example.com"

 

 2) Prefixで指定

Prefixで指定したActionにRedirectする。

redirect_to Prefix名
redirect_to root_path

 

 3) アクションで指定

同一Controllerの指定したActionにRedirectする。

redirect_to action: :Action名
redirect_to action: :new

 

 4) 指定したControllerのアクションで指定

指定したControllerの指定したActionにRedirectする。

redirect_to controller: :Controller名, action: :アクション名
redirect_to controller: :users, action: :show

 

 5) アクションの個別のリソースを指定

RedirectされるActionがshowのidが1のResourceの指定は

redirect_to controller: :users, action: :show, id: 1

 

 6) 前のページを指定

現在開いている前のパージへRedirectする。

redirect_to :back

 

redirectメソッド : satausを指定してリダイレクトする方法

 - statusコードとはwebサーバーからのレスポンスの意味を数字3桁で表したコード。

 - redirect_toではstatusコードを指定して実行させることができる。

シンボル コード 説明
:ok 200 requestが成功し、正常なreponseが返った
:create 201 requestが成功し、リソースの生成に成功
:moved_permanently 301 resourceが永久的にレダイレクトされる
:found 302 resourceが意図的にリダイレクトされる
:see_other 303 resourceが別の場所にある
:bad_request 400 不正なrequestである
:unauthorized 401 未承認のためにrequestは失敗
:forbidden 403 アクセス禁止のためにrequestは失敗
:not_found 404 resourceが存在しないためrequestは失敗
:method_not_allowed 405 HTMLメソッドが許可されていない
:internal_server_error 500 内部サーバエラー

 


redirectメソッド例題)UsersControllerでユーザーが存在しない場合Main Pageにリダイレクトし、ユーザーの情報を成功的にUpdateした時はユーザーのPageにリダイレクトする。

 

user_controller.rb

Class UsersController < ApplicationController
	def show
    	@user = User.find_by(id: params[:id])
        redirect_to(root_url, notice: "User not found") if @user.nil?
    end
    
    def update
    	@user = User.find(params[:id])
        if @user.update(user_params)
        	redirect_to user_path(@user), notice: 'User info updated'
        else
        	render :edit
    end
end

 


Redirectした時のメッセージを表紙する

 - redirect_toでnoticeやalertを使うことによりflashメッセージを設定し、メッセージを表紙させることができる。

 - flashメッセージとは、Actionの実行後に一度だけ表紙することができるメッセージ

 例)flash[:success] = '登録に成功しました'

redirect_to root_path, notice: 'ユーザー登録に成功しました'

 

View fileに表紙させたい場所に記述する。

<%= notice %>

 


redirect_toメソッドとrenderメソッドの違い

 - redirect_toメソッドとrenderメソッドの違いは、Actionを通してViewを表紙させるか、されないかの違いがある。

 - redirect_to は1回指定したActionを実行して、そのActionに対応したViewを表紙する。

 

  目的
redirect_to ユーザーを他のURLにリダイレクションする。HTTP状態コード302 ユーザーがあるタスク(例:フォームの提出)を完了したときに新しいページに移動する必要がある場合に使用。

 

  目的
render 特定のViewをレンダリングしてユーザーに表紙する。 特にフォーム提出過程でエラーが発生した時、同じページにエラーメッセージを表示する時に使用。

 

def create
	@user = User.new(user_param)
    if @user.save
    	redirect_to user_path(@user), notice: "User successfully created"
    else
    	render :new
    end
end

@user.saveが成功した時、ユーザーはuser_path(@user)にリダイレクションする。

@user.saveが失敗したら、newにレンダリングされる。

redirect_toメソッドとrenderメソッドの主な違い
HTTP要請 redirect_toは新しいHTTP要請を発生させる反面、renderは現在のHTTP要請中で行われる。
Data保持 redirect_toを使うと全てのインスタンス変数が初期化され、新しいパージが始める。
renderは現在のインスタンス変数を保持する。
URL redirect_toはURLを変更する。
renderはURLを変更しない。

 

 

728x90
반응형
LIST