0

I am trying to make an app with Rails 4. I use CanCanCan for abilities and Role_Model for roles.

I am having a difficult time in finding the basic rules for conditional statements. I don't understand how many 'ends' you need after an 'if/elsif/else' statement and also, why i get so many errors asking for more end statements.

This is my ability.rb (I have deleted some of the content of the methods to make this file shorter):

class Ability
  include CanCan::Ability


  def initialize(user)

      alias_action :create, :read, :update, :destroy, :to => :crud

      alias_action :create, :read, :to => :cr

      alias_action :create, :read, :update, :to => :cru

      alias_action :update, :destroy, :to => :ud

    # Define abilities for the passed in user here. For example:
    #
    user ||= User.new # guest user (not logged in)

      new_registrant


      maintain_profile

    elsif user.try(:profile).present? && user.profile.has_role?(:student)

      student_abilities

    elsif  user.try(:profile).present? && user.profile.has_role?(:educator)

      educator_abilities

    elsif user.try(:profile).present? && user.profile.has_role?(:researcher)

      researcher_abilities

    elsif user.try(:profile).present? && user.profile.has_role?(:ktp)
      ktp_abilities


    elsif user.try(:profile).present? && user.profile.has_role?(:faculty_manager)

      faculty_manager_abilities
      read_program_invitation

    elsif user.try(:profile).present? && user.profile.has_role?(:ip_asset_manager)

      ip_asset_manager_abilities

    elsif user.try(:profile).present? && user.profile.has_role?(:sponsor)
      sponsor_abilities
      read_program_invitation

    elsif user.try(:profile).present? && user.profile.has_role?(:project_manager)

      project_manager_abilities

    elsif user.try(:profile).present? && user.profile.has_role?(:representative)

     representative_abilities


    elsif user.try(:profile).present? && user.profile.has_role?(:grantor)

      grantor_abilities


    elsif user.try(:profile).present? && user.profile.has_role?(:investor)

      investor_abilities

    elsif user.try(:profile).present? && user.profile.has_role?(:adviser)

      adviser_abilities

    elsif user.try(:profile).present? && user.profile.has_role?(:innovation_consultant)

      innovation_consultant_abilities

    elsif user.try(:profile).present? && user.profile.has_role?(:participant)

      participant_abilities

    elsif user.try(:profile).present? && user.profile.has_role?(:guest)

      guest_abilities

    elsif user.try(:profile).present? && user.profile.has_role?(:manager)

      manager_abilities

    elsif user.admin?
        can :manage, :all
    end

  end


def new_registrant



      can :read, ActiveUserPolicy, [:last_login]
      can :read, PolicyPrivacy
      can :read, PolicyUser
      can :read, Newsletter

      cannot :read, Profile.total_privacy_control

QUESTION 1: PLURAL (FOR DB TABLE NAME OR SINGULAR FOR MODEL CLASS NAME???

      cannot :read, Profiles.member_privacy_control
end


def maintain_profile
   new_registrant

   can :crud, Profiles, :user_id => user.id #[for themselves]

   can :read, Profiles
   cannot :read, Profile.total_privacy_control
   cannot :read, Profiles.member_privacy_control
   can :read, Feedbacks.visible 

end

def community_abilities
  can :ud, ArticleComments.own

end

def project_qanda
  # can ask questions on projects
      can :cr, ProjectQuestions if can? :read, Projects


    # can update & their own destroy  unanswered PQs
      can :ud, ProjectQuestions.user_question.unanswered 

    #can read answer to Q     
      can :read, ProjectAnswers, if can? :read, ProjectQuestions 

end

def program_qanda
end

def proposal_qanda
end

def student_abilities

    maintain_profile
    community_abilities
    project_qanda
    proposal_qanda

end

def educator_abilities

  maintain_profile
  community_abilities
  proposal_qanda

end


def researcher_abilities
  maintain_profile
  community_abilities 
  proposal_qanda 

end

def ktp_abilities
  maintain_profile
  community_abilities
  proposal_qanda

end

def faculty_manager_abilities
  maintain_profile
  community_abilities

end

def ip_asset_manager_abilities
  maintain_profile
  community_abilities
  #answer diligence questions

end

def sponsor_abilities
  maintain_profile
  community_abilities
  project_qanda
  program_qanda
  # create diligence report
end


def representative_abilities
  maintain_profile
  community_abilities
end

def grantor_abilities
  maintain_profile
  community_abilities

 end

def grant_application_abilities
end



def investor_abilities
end

def adviser_abilities
end

def innovation_consultant_abilities
  maintain_profile
  community_abilities
end

def participant_abilities
  maintain_profile
  community_abilities

end

def guest_abilities
  maintain_profile

end

def manager_abilities


end

def transaction_abilities
end

def diligience_abilities

end

def job_creator
end

def job_application
end

def feedback_abilities
end

def project_manager_abilities
  maintain_profile

end
end

QUESTION 2: I ADDED ALL OF THE BELOW END STATEMENTS BECAUSE I KEEP GETTING SYNTAX ERRORS SAYING UNEXPECTED END OF INPUT, EXPECTING KEYWORD END. I KEPT ADDING ONE BY ONE UNTIL THAT ERROR STOPPED COMING UP BUT I DON'T UNDERSTAND WHAT THESE STATEMENTS ARE ENDING OR WHY THEY ARE REQUIRED. THERE ISN'T THE SAME NUMBER OF ANYTHING ELSE IN THIS FILE. HOW DO I FIND THE RULES ON WHERE TO PUT END STATEMENTS?

  end
  end
  end
  end
 end
  end
  end
  end 
  end
  end
  end
  end
  end
Mel
  • 2,481
  • 26
  • 113
  • 273

1 Answers1

0

It will solve your question 2 Check your if else syntax. It should be like this

If..Elsif...end Syntax :

if conditional [then]
      code...
[elsif conditional [then]
      code...]...
[else
      code...]
end

Ex

if var == 10
  print “Variable is 10”
elsif var == “20”
  print “Variable is 20”
else
  print “Variable is something else”
end
milind phirake
  • 493
  • 3
  • 8
  • Hey, thanks for this. I see what you mean, I didn't know it was looking for then, but I don't follow your example, you haven't used the word then?? Also, someone just informed me on a previous post that the last else should be elsif. I changed it following that feedback – Mel Oct 09 '15 at 05:00