如何删除作用于rolify中的资源实例的角色?

时间:2018-05-16 13:29:32

标签: ruby-on-rails rolify

我使用以下方法实现了用户 - 成员资格 - 组织关系: 我使用rolify来实现组织中用户的角色。

class User < ApplicationRecord
    rolify
    has_many :memberships, :dependent => :destroy
    has_many :organizations, through: :memberships
end


class Membership < ApplicationRecord
    belongs_to :user
    belongs_to :organization
end

class Organization < ApplicationRecord
    resourcify
    has_many :memberships, :dependent => :destroy
    has_many :users, through: :memberships
end

然后,每当我在OrganizationControllers中调用def unjoin时:

def join
    @organization = Organization.find(params[:id])

    if @organization.users.exists?(current_user.id)
        flash[:notice] = 'Already a Member'
        redirect_to @organization
    else
        @organization.users << current_user
        current_user.add_role :member, @organization

        if current_user == @organization.users.last
           flash[:notice] = 'Successfully Joined In'
           redirect_to @organization
        else
           flash[:notice] = 'Unable to Join'
           redirect_to @organization
        end
    end
end

def unjoin      
    @organization = Organization.find(params[:id])
    if @organization.users.exists?(current_user.id)

        current_user.remove_role :member, @organization         #Error 
        @organization.users.delete(current_user)

        if @organization.users.exists?(current_user.id)
            flash[:notice] = 'Unable to unjoin'
            redirect_to @organization
        else
            flash[:notice] = 'Successfully Unjoined'
            redirect_to @organization
        end
    else
        flash[:notice] = 'Not Yet A Member'
        redirect_to @organization
    end
 end

我在OrganizationsController #ninjoin中得到 NoMethodError 未定义的方法`用户&#39;对于#, 在线: current_user.remove_role:member,@organization

0 个答案:

没有答案
相关问题