ユーザーがログインした後、そのロールに応じていくつかのページにリダイレクトされ、 this、this、 および thisを読みましたが、正しく実行できないようです。それぞれを試してみましたそれぞれが私に問題を与えました。
私が望むのは、ユーザーがログインした後、彼/彼女を x_path にリダイレクトすることだけです。技術的には機能しますが、ログイン後、redirect_user テンプレートをロードしようとします。
アプリケーションコントローラー
class ApplicationController < ActionController::Base
before_filter :authenticate_user!
protect_from_forgery
rescue_from CanCan::AccessDenied do |e|
redirect_to new_user_session_path, alert: e.message
end
def redirect_user
if current_user.role == 'Administrator'
redirect_to rails_admin_path
elsif current_user.role == 'C1' or 'D1' or 'M1' or 'M2'
redirect_to upload_files_path
end
end
end
Routes.rb
Siteconfigurationlistgenerator::Application.routes.draw do
devise_for :users
match '/' => 'application#redirect_user'
能力
class Ability
include CanCan::Ability
def initialize(user)
#Define abilities for the passed in user here.
user ||= User.new #guest user (not logged in)
#a signed-in user can do everything
if user.role == 'Administrator'
#an admin can do everything
can :manage, :all
can :access, :rails_admin # grant access to rails_admin
can :dashboard # grant access to the dashboard
elsif user.role == '1' or 'M1' or 'D1' or 'M2'
can :manage, [MaterialList, Inventory, UploadFiles, DownloadTemplate]
cannot :access, :rails_admin
cannot :dashboard
can :access, [:upload_files, :download_template]
end
end
end
現在、私が持っているように、テンプレートが見つからないなどのメッセージが表示され、views/application フォルダーで redirect_user テンプレートが検索されますが、そうではなく、単にリダイレクトする必要があります