プログラミングの勉強日記
2020年7月19日 Progate Lv.211
Ruby on RailsⅧ
アクション側で共通の変数の定義
各アクションに対尾したビューファイルはapplication.html.erbの<%= yield %>
の部分に代入され表示される。各コントローラの全アクションで共通する処理がある場合には、before_action
を使うと良い。before_action
を用いることで、アクションが呼び出される際に必ずbefore_action
の処理が実行されるので、全アクションで共通する処理を1か所にまとめられる。
すべてのコントローラで共通する処理はapplicationコントローラにまとめられる。set_current_user
メソッドを定義し、before_action
に指定する。
before_action :set_current_user
@current_user=User.find_by(id: session[:user_id])
end
ログインしていないときのアクセス制限
ログインしていない場合でもURLを直接入力するとアクセスできてしまう。@current_user
がいない場合にはログインページにリダイレクトする。この処理は他のアクションや他のコントローラでも使いたいので、applicationコントローラとbefore_action
を用いて処理を共通化する。
def autheniticate_user
if @current_user==nil
flash[:notice]="ログインが必要です"
redirect_to("/login")
end
end
onlyを用いて各コントローラでbefore_action
を使うことで指定したアクションのみでメソッドを実行できる。各コントローラはapplicationコントローラを継承しているので、継承元のメソッドが使える。
before_action :autheniticate_user, {only: [:edit, :update]}
class ApplicaionController < ActionController::Base
def set_current_user
@current_user=User.find_by(id :session[:user_id])
end
...
ログインユーザのアクセス制限
applicationコントローラ内にログインユーザを制限するという意味のforbid_login_user
メソッドを作成。ログインユーザが存在する場合、投稿一覧ページにリダイレクトする。メソッドの実行出にはbefore_action
を用いてonlyで適応したいアクションを指定する。
def fobid_login_user
if @current_user
flash[:notice]="ログインしています"
redirect_to("/posts/index")
end
end
before_action :forbid_login_user, {only: [:top]}
before_action :forbid_login_user, {only: [:name, :create, :login_form, :login]}
ユーザ編集ページのリンク
ユーザ詳細画面の編集リンクを自分の詳細画面のときだけに表示されるようにする。ユーザ詳細ページでログインしているユーザでない場合には編集ページへのリンクを非表示にする。
<%# 詳細ページのユーザidとログインしているユーザのidが等しい場合 %>
<% if @user.id==@current_user.id %>
<% link_to("編集", "users/#{@user.id}/edit") %>
<% end %>
このままだと編集ページのURLを直接入力すれば編集ページに入れてしまう。なので、usersコントローラのeditアクションとupdateアクションに制限をかける。正しいユーザかを確かめるという意味のensure_current_user
メソッドを用意して、ログイン中のユーザのidと編集したいユーザのidが等しいかを判定する。
ログイン中のユーザのidは@current_user.id
に編集したいユーザのidはparams[:id]
にそれぞれ代入されている。params[:id]
で取得できる値は文字列であり、数値である@current_user.id
で比較してもfalseとなる。to_iメソッドを用いると文字列を数値に変換できる。
before_action :ensure_current_user, {only: [:edit, :update]}
def ensure_current_user
if @current_user.id != params[:id].to_i
flash[:notice]="権限がありません"
redirect_to("/posts/index")
end
end