簡體   English   中英

當我想與cancancan一起使用時,rails設計未定義的方法用戶名

[英]rails devise undefined method username when I want to use it with cancancan

我想在Rails中實現基於角色的授權。 登錄時,我使用了gem dev,效果非常完美。 但是在我在本教程中包含cancancan的一些代碼后,我得到了一個錯誤的未定義方法用戶名,當我刪除用戶名時,我得到了一個錯誤的未定義方法電子郵件...

這是我的代碼:

Capacity.rb(管理具有特定角色的用戶可以做什么)

    class Ability

  include CanCan::Ability

  def initialize(user)

      user ||= User.new

     if user.role?(:admin)
       can :manage, :all
     elsif user.role?(:mitarbeiter)
       can :manage, :documents
       can :manage, :entries
     end

  end

end

我的user.rb的一部分

      ROLES = {0 => :admin, 1 => :mitarbeiter}

  attr_reader :role :

  def initialize(role_id = 0)
    @role = ROLES.has_key?(role_id) ? ROLES[role_id] : ROLES[0]

  end

  def role?(role_name)
    role == role_name
  end

我application_controller.rb的一部分

  protect_from_forgery with: :exception

  check_authorization

  rescue_from CanCan::AccessDenied do |exception|
    flash[:warning] = exception.message
    redirect_to root_path
  end

  private

  def current_user
    User.new(session[:id])
  end

  helper_method :current_user

我真的無法理解是什么錯誤...我以為我必須在user.rb中將@user設置為@role,但這無濟於事

我建議刪除User#initialize方法和attr_reader :role聲明,並創建自己的role方法,如下所示:

def role
  @role ||= ROLES.has_key?(role_id) ? ROLES[role_id] : ROLES[0]
end

def role?(role_name)
  role == role_name
end

通過這種方式,您可以實現預期的目的,而不會覆蓋ActiveRecord的User#initialize方法。

上面的代碼假定, role_id存儲在數據庫中的role_id列中。

我找到了解決方案,而且role_id也正確存儲在數據庫中。 這是我的解決方案:

我的新能力.rb(不僅限於測試功能)

    class Ability

  include CanCan::Ability

  def initialize(user)

     user ||= User.new

     if user.has_role? :admin
       can :manage, :all
     elsif user.has_role? :mitarbeiter
       can :manage, :documents
       can :manage, :entries
     else
        can :read, :all
     end

  end

end

新的user.rb

      ROLES = %i[admin mitarbeiter]

  def roles=(roles)
    roles = [*roles].map { |r| r.to_sym }
    self.role_id = (roles & ROLES).map { |r| 2**ROLES.index(r) }.inject(0, :+)
  end

  def roles
    ROLES.reject do |r|
      ((role_id.to_i || 0) & 2**ROLES.index(r)).zero?
    end
  end

  def has_role?(role)
    roles.include?(role)
  end

新的application_controller.rb部分

  def configure_permitted_parameters
    added_attrs = [:username, :email, :password, :password_confirmation, :remember_me, roles: [] ]
    devise_parameter_sanitizer.permit :sign_up, keys: added_attrs
    devise_parameter_sanitizer.permit :account_update, keys: added_attrs
  end

在這里,我將角色授予用戶views / devise / registrations / new.html.erb

    <% for role in User::ROLES %>
      <%= check_box_tag "user[roles][#{role}]", role, @user.roles.include?(role), {:name => "user[roles][]"}%>
      <%= label_tag "user_roles_#{role}", role.to_s.humanize %><br />
    <% end %>
    <%= hidden_field_tag "user[roles][]", "" %>

我還刪除了一些gem(回形針,roo,rolify),這可能是我在Gemfile中遇到的一個問題:

    gem 'devise', github: 'plataformatec/devise'
    gem 'cancancan'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM