我想將一個phone用戶欄位更改為必填欄位。當現有用戶沒有設定此欄位時(不必事先提供電話號碼),它應該重定向到user_edit頁面并Phone is required在表單下方顯示訊息。我正在使用Punditgem 進行授權:
class ApplicationController < ActionController::Base
include Pundit
rescue_from Pundit::NotAuthorizedError, with: :login_not_authorized
private
def login_not_authorized
flash[:alert] = 'You are not authorized to perform this action.'
redirect_to(request.referer || root_path)
end
end
如何檢查現有用戶是否有電話號碼,如果沒有,將此用戶移動到他的 EDIT 頁面并Phone is required在表單下方顯示錯誤訊息?
uj5u.com熱心網友回復:
你可以嘗試使用這樣的東西:
def login_not_authorized
if current_user&.phone.blank?
flash[:alert] = 'You must provide your phone number.'
redirect_to(user_edit_path)
else
flash[:alert] = 'You are not authorized to perform this action.'
redirect_to(request.referer || root_path)
end
end
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/404415.html
標籤:
