Ruby on rails Rails 4、Desive、CanCan和Rails_管理问题?

Ruby on rails Rails 4、Desive、CanCan和Rails_管理问题?,ruby-on-rails,ruby-on-rails-4,devise,cancan,rails-admin,Ruby On Rails,Ruby On Rails 4,Devise,Cancan,Rails Admin,我一直在尝试设置rails_admin并使用rails 4.0.4,但不幸的是,我遇到了一些问题。我有一个Desive生成的用户模型,之后添加了管理员作为布尔值。但是,即使用户是管理员并且需要访问rails_admin面板,我也会获得未经授权的访问权限。这就像当前用户无法正确地在ability.rb中传递一样。不确定这是Rails 4的问题还是我做错了什么 这是代码,有一点(丑陋?)的解决办法,但我需要一个更优雅的解决方案。谢谢 能力.rb class Ability include Can

我一直在尝试设置rails_admin并使用rails 4.0.4,但不幸的是,我遇到了一些问题。我有一个Desive生成的用户模型,之后添加了管理员作为布尔值。但是,即使用户是管理员并且需要访问rails_admin面板,我也会获得未经授权的访问权限。这就像当前用户无法正确地在ability.rb中传递一样。不确定这是Rails 4的问题还是我做错了什么

这是代码,有一点(丑陋?)的解决办法,但我需要一个更优雅的解决方案。谢谢

能力.rb

class Ability
  include CanCan::Ability

  def initialize(user)
    # Define abilities for the passed in user here. For example:
    #
      user = User.current # guest user (not logged in)
      if user.admin? 
        can :manage, :all
        can :access, :rails_admin # needed to access RailsAdmin
        can :dashboard            # dashboard access
      else
        can :read, :all
      end
  end
end
class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable     

  def self.current
    Thread.current[:user]
  end      

  def self.current=(user)
    Thread.current[:user] = user
  end
end
class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception

  before_filter :set_current_user

  def set_current_user
    User.current = current_user
  end
end
user.rb

class Ability
  include CanCan::Ability

  def initialize(user)
    # Define abilities for the passed in user here. For example:
    #
      user = User.current # guest user (not logged in)
      if user.admin? 
        can :manage, :all
        can :access, :rails_admin # needed to access RailsAdmin
        can :dashboard            # dashboard access
      else
        can :read, :all
      end
  end
end
class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable     

  def self.current
    Thread.current[:user]
  end      

  def self.current=(user)
    Thread.current[:user] = user
  end
end
class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception

  before_filter :set_current_user

  def set_current_user
    User.current = current_user
  end
end

谢谢

我注意到两件事,它们都很奇怪(不确定其中是否有一件是核心问题)

1) 你不需要在任何地方进行身份验证

您的ApplicationCantoller应该具有:

before_filter :authenticate_user!
你可以这样做(我相信你不需要在Desive中这样做)

2) 您是否尝试重新启动服务器


我相信RailsAdmin不会自动重新加载(你要么需要破解一个特殊的破解,要么重新启动一个服务器)

我注意到两件事,这两件事很奇怪(不确定它们是否是核心问题)

1) 你不需要在任何地方进行身份验证

您的ApplicationCantoller应该具有:

before_filter :authenticate_user!
你可以这样做(我相信你不需要在Desive中这样做)

2) 您是否尝试重新启动服务器


我相信RailsAdmin不会自动重新加载(你要么需要破解一个特殊的破解程序,要么重新启动一个服务器)

似乎可以,但在添加rails\u admin之前,你首先需要一个设计模型

此外,这需要在rails_admin初始值设定项中启用:


config.current\u user\u方法(&:current\u user)

似乎有效,但在添加rails\u admin之前,首先需要设计模型

此外,这需要在rails_admin初始值设定项中启用:


配置当前用户方法(&:当前用户)

到目前为止,Cancan尚未为rails4做好准备。您可以使用它,但您可能会遇到一些问题。到目前为止,Cancan尚未为rails4做好准备。您可以使用它,但您可能会遇到一些问题。我认为它不需要:验证用户!因为我正在通过设计表单登录,然后尝试打开/admin。问题是,它不会传递当前用户/会话或类似的内容。我完全按照这里说的做了:在发布之前,还有其他来源。恐怕这与我的Rails版本有关。它与我原来的帖子中的解决方案配合使用,但与官方指南不配合。也将检查重新加载,谢谢。:)我认为它不需要:验证用户!因为我正在通过设计表单登录,然后尝试打开/admin。问题是,它不会传递当前用户/会话或类似的内容。我完全按照这里说的做了:在发布之前,还有其他来源。恐怕这与我的Rails版本有关。它与我原来的帖子中的解决方案配合使用,但与官方指南不配合。也将检查重新加载,谢谢。:)我有同样的问题,添加config.current\u user\u方法(&:current\u user)修复了它。我有同样的问题,添加config.current\u user\u方法(&:current\u user)修复了它。