Rails,Devise,角色模型和CanCanCan-定义能力 [英] Rails, Devise, Role Model and CanCanCan - defining abilities

查看:77
本文介绍了Rails,Devise,角色模型和CanCanCan-定义能力的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Rails 4制作一个Web应用程序.

I am using Rails 4 to make a web app.

我正在尝试使用CanCanCan定义各种角色的能力.

I am trying to use CanCanCan to define abilities for the various roles.

我有一个用户模型和一个个人档案模型.每个用户可以有许多个人资料.每个个人资料可以扮演不同的角色.

I have a User model and a Profile model. Each user can have many profiles. Each profile can have a different role.

在我的Profile.rb中,我的角色(使用Role Model gem)定义为:

In my Profile.rb, I have defined my roles (using Role Model gem) as:

  include RoleModel

roles :admin, :manager, # coalfacer
        :student, :educator, :researcher, :ktp, :faculty_manager, :ip_asset_manager,  # for universities
        :sponsor, # for industry
        :project_manager, :representative, # for both uni and industry
        :grantor, :investor, :adviser, :innovation_consultant, :panel_member, # external
        :participant, :guest # public

  roles_attribute :roles_mask

在我的Capacity.rb中,我试图将第一个能力定义为:

In my ability.rb, I am trying to define the first ability, as:

   user ||= User.new # guest user (not logged in)

      #users who are not signed in can read publicly available projects

      can :read, Project, {:active => true, :closed => false,  &&  Project.sweep.disclosure.allusers: true}

在我的Projects表中,我有一个名为:closed的布尔属性.

In my Projects table, I have a boolean attribute called :closed.

我也有扫描和披露"模型.关联是:

I also have models for Sweep and Disclosure. The associations are:

Project.rb:
 has_one :sweep
  accepts_nested_attributes_for :sweep

Sweep. rb

belongs_to :project
has_one :disclosure
accepts_nested_attributes_for :disclosure

Disclosure.rb

belongs_to :sweep

我的披露表具有一个名为:allusers的布尔属性.

My disclosures table has a boolean attribute called :allusers.

如果Projects.closed为false并且project.sweep.disclosure.allusers为true,那么我想允许访客阅读该项目.

If Projects.closed is false AND project.sweep.disclosure.allusers is true, then I want to allow guests to read the Project.

当我如上所述尝试时,出现错误消息:

When I try as I have set out above, I get an error which says:

undefined method `id' for nil:NilClass

它是指我的Project show视图中的一行,内容为:

It's referring to a line in my Project show view, which says:

<span class="editproject">    <% if current_user.id == @project.creator_id %>
          <%= link_to 'Edit Project', edit_project_path(@project) %> <% end %> </span>

在尝试定义能力之前,这不会引起错误.另外,在项目索引中返回的几个项目是不符合我在我的Capacity.rb中指定的标准的项目(例如Disclosure.allusers未设置为true).

Before trying to define abilities, this did not cause an error. Also, several of the projects returned in the index of projects are projects which do not meet the criteria I specified in my ability.rb (e.g. Disclosure.allusers is not set as true).

有人能看到我在定义上述能力时做错了什么吗?

Can anyone see what I've done wrong in defining this ability as above?

推荐答案

错误为NoMethodError

The error is NoMethodError

undefined method `id' for nil:NilClass

如错误所述,Rails尝试在nil:NilClass上调用current_user,这意味着您的 id 对象为nil.

As the error says, that Rails is trying to call current_user on nil:NilClass, which implies that your id object is nil.

因此,您应该使 current_user

@current_user

这篇关于Rails,Devise,角色模型和CanCanCan-定义能力的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆