0

I am using Rails 4 to make a web app.

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.

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

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}

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

My disclosures table has a boolean attribute called :allusers.

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 `profile' for nil:NilClass

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

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?

This is not the same problem as I set out in my related question with the same title.

Mel
  • 2,481
  • 26
  • 113
  • 273
  • possible duplicate of [Rails, Devise, Role Model and CanCanCan - defining abilities](http://stackoverflow.com/questions/32304996/rails-devise-role-model-and-cancancan-defining-abilities) – nayiaw Aug 31 '15 at 08:48
  • Hi Nayiaw, I asked that question. When I implemented the solution, I got a new error and am now asking for help with this different problem. – Mel Aug 31 '15 at 08:52
  • The posted content is almost the same problem as the previous problem, which I think that question should not have a best answer before you totally solve the problem, i.e the `@current_user` is still `nil`. – nayiaw Aug 31 '15 at 10:48
  • It's a different problem – Mel Aug 31 '15 at 10:49

0 Answers0