私はゼロからユーザー認証を構築しており、ユーザーの役割に取り組んできました。権限の定義方法がわからないため、権限の部分で立ち往生しています。「before_filter :authorize」をコントローラーに追加すると、すべてのページで「初期化されていない定数 ApplicationController::Permission」エラーが表示されます。
application_controller.rb ファイルからエラーが発生していることはわかっています。
def current_permission
@current_permission || Permission.new(current_user)
end
def authorize
if !current_permission.allow?(params[:controller], params[:action])
redirect_to root_url, alert: "Not authorized."
end
パーミッション.rb:
Class Permission < Struct.new(:user)
def allow?(controller, action)
if user.nil?
controller == "galleries" && action.in?(%w[index show])
elsif user.admin?
true
else
controller == "galleries" && action != "destroy"
end
end
私が知らないのは、アプリで権限を適切に定義して、そのエラーを受け取らないようにする方法です。誰にもアイデアはありますか?