2016-07-13 10 views
0

Ruby on RailsアプリケーションでDeviseとPunditの宝石を使用しています。 URL「myapp.com/admin」で非技術チームメンバーの簡単な管理インターフェースを設定するためにRailsAdminを実装しようとしていますが、このURLに行くと次のエラーが発生します:RailsAdminがPunditと連携していない - Pundit :: AuthorizationNotPerformedError at/RailsAdmin :: MainController

評論家:: AuthorizationNotPerformedError/ RailsAdmin ::メインコントローラで

このエラーは、次の方法でファイルのlib/pundit.rbによって発生します

def verify_authorized 
 
    raise AuthorizationNotPerformedError, self.class unless pundit_policy_authorized? 
 
    end

これはPunditのポリシーにリンクされていますが、実行する変更を見つけることができません。ここで

は私が今まで試したものです:私のGemfileで

、私がインストールされている:

宝石 "rails_admin"

宝石"rails_admin_pundit"、:github => "sudosu/rails_admin_pundit"

その後、私の端末では、私は

を走った 'バンドル' 私の初期化子で

、私が持っている:私は

RailsAdmin.config do |config| 
 
    ## == Devise == 
 
    config.authenticate_with do 
 
    warden.authenticate! scope: :user 
 
    end 
 
    config.current_user_method(&:current_user) 
 

 
    ## == Pundit == 
 
    config.authorize_with :pundit 
 
end

をcontrollers/application_controller.rb、私は持っています次のコード:

class ApplicationController < ActionController::Base 
 
    # Prevent CSRF attacks by raising an exception. 
 
    # For APIs, you may want to use :null_session instead. 
 
    protect_from_forgery with: :exception 
 

 
    before_action :configure_permitted_parameters, if: :devise_controller? 
 
    before_action :authenticate_user! 
 
    include Pundit 
 

 
    after_action :verify_authorized, except: :index, unless: :devise_controller? 
 
    after_action :verify_policy_scoped, only: :index, unless: :devise_controller? 
 

 
    rescue_from Pundit::NotAuthorizedError, with: :user_not_authorized 
 

 
    def user_not_authorized 
 
    flash[:alert] = "You are not authorized to perform this action." 
 
    redirect_to(root_path) 
 
    end 
 

 
    def default_url_options 
 
    { host: ENV['HOST'] || 'localhost:3000' } 
 
    end 
 

 
    private 
 

 
    def configure_permitted_parameters 
 
    devise_parameter_sanitizer.for(:sign_up) << [:first_name, :last_name, :bio, :email, :password, :password_confirmation, :photo, :photo_cache] 
 
    devise_parameter_sanitizer.for(:account_update) << [:first_name, :last_name, :bio, :email, :password, :password_confirmation, :photo, :photo_cache] 
 
    end 
 

 
end

最後に、/ application_policy.rb私のポリシーで、私が持っている( '末端DEF rails_admin?' メソッドで参照してください):

class ApplicationPolicy 
 
    attr_reader :user, :record 
 

 
    def initialize(user, record) 
 
    @user = user 
 
    @record = record 
 
    end 
 

 
    def index? 
 
    false 
 
    end 
 

 
    def show? 
 
    scope.where(:id => record.id).exists? 
 
    end 
 

 
    def create? 
 
    false 
 
    end 
 

 
    def new? 
 
    create? 
 
    end 
 

 
    def update? 
 
    false 
 
    end 
 

 
    def edit? 
 
    update? 
 
    end 
 

 
    def destroy? 
 
    false 
 
    end 
 

 
    def scope 
 
    Pundit.policy_scope!(user, record.class) 
 
    end 
 

 
    class Scope 
 
    attr_reader :user, :scope 
 

 
    def initialize(user, scope) 
 
     @user = user 
 
     @scope = scope 
 
    end 
 

 
    def resolve 
 
     scope 
 
    end 
 
    end 
 

 
    def rails_admin?(action) 
 
    case action 
 
     when :dashboard 
 
     user.admin? 
 
     when :index 
 
     user.admin? 
 
     when :show 
 
     user.admin? 
 
     when :new 
 
     user.admin? 
 
     when :edit 
 
     user.admin? 
 
     when :destroy 
 
     user.admin? 
 
     when :export 
 
     user.admin? 
 
     when :history 
 
     user.admin? 
 
     when :show_in_app 
 
     user.admin? 
 
     else 
 
     raise ::Pundit::NotDefinedError, "unable to find policy #{action} for #{record}." 
 
    end 
 
    end 
 
end

あなたは何が欠けているのか分かりませんか? ありがとうございます。

答えて

0

私は私の問題を解決しました。 rails_admin_pundit gemに少し不具合があるようです。 次の変更は、rails_admin_pundit gemのGitHubリポジトリにコミットされていますが、今までは無視されました!代わりに、呼び出し元の私のGemfileで

、:

宝石 "rails_admin_pundit"、:githubの=> "sudosu/rails_admin_pundit"

私は呼ば:

宝石「rails_admin_pundit "、:github =>" Samy-Amar/rails_admin_pundit "

基本的には、

のlib/rails_admin /拡張/評論家に1つの余分な行を追加してコミット/ authorization_adapter.rb

あなたがこのページにこの小さな変更の詳細を見ることができます:https://github.com/Samy-Amar/rails_admin_pundit/commit/4017395e5e715e2f1fd95918bc4bb9b76d272b25

これが助けてくれることを願っています!

関連する問題