cancan - Rails, Devise, Role Model and CanCanCan - defining abilities -
i using rails 4 make web app.
i trying use cancancan define abilities various roles.
i have user model , profile model. each user can have many profiles. each profile can have different role.
in profile.rb, have defined roles (using role model gem) as:
include rolemodel roles :admin, :manager, # coalfacer :student, :educator, :researcher, :ktp, :faculty_manager, :ip_asset_manager, # universities :sponsor, # industry :project_manager, :representative, # both uni , industry :grantor, :investor, :adviser, :innovation_consultant, :panel_member, # external :participant, :guest # public roles_attribute :roles_mask
in ability.rb, trying define first ability, as:
user ||= user.new # guest user (not logged in) #users not signed in can read publicly available projects can :read, project, {:active => true, :closed => false, && project.sweep.disclosure.allusers: true}
in projects table, have boolean attribute called :closed.
i have models sweep , disclosure. 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 boolean attribute called :allusers.
if projects.closed false , project.sweep.disclosure.allusers true, want allow guests read project.
when try have set out above, error says:
undefined method `id' nil:nilclass
it's referring line in project show view, says:
<span class="editproject"> <% if current_user.id == @project.creator_id %> <%= link_to 'edit project', edit_project_path(@project) %> <% end %> </span>
before trying define abilities, did not cause error. also, several of projects returned in index of projects projects not meet criteria specified in ability.rb (e.g. disclosure.allusers not set true).
can see i've done wrong in defining ability above?
the error nomethoderror
undefined method `id' nil:nilclass
as error says, rails trying call current_user on nil:nilclass, implies id
object nil.
so, should make current_user
@current_user
Comments
Post a Comment