【问题标题】:Rails_admin and rails_admin_pundit error after upgrade to 5.2.1升级到 5.2.1 后 Rails_admin 和 rails_admin_pundit 错误
【发布时间】:2018-10-09 20:19:55
【问题描述】:
NoMethodError at /
protected method `policy' called for #<RailsAdmin::MainController:0x007ff5e5d1a528>
Did you mean?  policies

这是它在错误页面中看到的第一件事(这是在 gemfile 代码中)

# This method is called to find authorization policy
        def policy(record)
          begin
            @controller.policy(record)
          rescue ::Pundit::NotDefinedError
            ::ApplicationPolicy.new(@controller.send(:pundit_user), record)
          end
        end
        private :policy

当我尝试访问 /admin 时出现此错误 - 没有任何改变,在 5.1.6 中工作正常。我没有更改 policy.rb 文件,我没有更改任何控制器代码,没有任何更改除了 gemfile 更新到 rails 5.2.1

我的申请政策..

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

我不知道发生这种情况的原因.. 在 Google 上找不到任何内容,并且 gem rails admin pundit 上次更新是一年前。

【问题讨论】:

    标签: ruby-on-rails rails-admin pundit


    【解决方案1】:

    确保你有

    config.parent_controller = "::ApplicationController"

    在您的config/initializers/rails_admin.rb 上,指明谁是父控制器。这为我解决了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-07-27
      • 1970-01-01
      • 2012-12-11
      • 1970-01-01
      • 1970-01-01
      • 2017-12-30
      • 1970-01-01
      相关资源
      最近更新 更多