0

我正在使用 Rails 4 制作一个网络应用程序。

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

我有一个用户模型和一个配置文件模型。每个用户可以有许多个人资料。每个配置文件可以具有不同的角色。

在我的 Profile.rb 中,我将我的角色(使用角色模型 gem)定义为:

  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

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

  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 表中,我有一个名为 :close 的布尔属性。

我也有 Sweep 和 Disclosure 的模型。这些协会是:

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 的布尔属性。

如果 Projects.close 为 false 并且 project.sweep.disclosure.allusers 为 true,那么我希望允许客人阅读该项目。

当我按照上面的说明尝试时,我收到一条错误消息:

undefined method `profile' for nil:NilClass

  <% if @current_user.profile.has_role? :student %>
      <%= link_to 'Edit Project', edit_project_path(@project) %> <% end %> </span>

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

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

这与我在具有相同标题的相关问题中提出的问题不同。

4

0 回答 0