0

I am trying to make an app with Rails 4. I use bootstrap, devise simple form and role_model gems.

profile.rb

class Profile < ActiveRecord::Base
  include RoleModel

  #reminder for roles (RoleModel): always add new roles to the end of this line - don't change the order
  roles :admin, :manager, 
        :student, :educator, :researcher, :ktp, :faculty_manager, :ip_asset_manager,  
        :sponsor, 
        :project_manager, :representative, # for both uni and industry
        :grantor, :investor, :adviser, :innovation_consultant, :panel_member,
        :participant, :guest, :pending

  # Other stuff goes here

end

Then in my profile view, I have a form where I ask users where they are from. I want to phrase the question differently depending on the user's role and I want to use a different array of options depending on that role. Eg, if the current user is a student, then the array should be populated with a list of universities.

<div class="col-md-3 col-md-offset-1">
  <% if current_user.profile.has_any_role?(:student, :educator, :researcher, :ktp, :faculty_manager, :ip_asset_manager) %>
    <%= f.label  'Select your university',   :class => 'question-project' %>
  <% elsif current_user.profile.has_role?(:sponsor) %>                  
    <%= f.label  'Select your organisation',   :class => 'question-project' %>    
  <% elsif current_user.profile.has_any_role?(:grantor, :investor, :adviser, :innovation_consultant, :panel_member) %>  
  <% end %>                
</div>

<div class="col-md-7">
  <div class="response-project">
    <% if current_user.profile.has_any_role?(:student, :educator, :researcher, :ktp, :faculty_manager, :ip_asset_manager) %>                
      <%= f.collection_select :university_id, University.all, :id, :name, prompt: 'Select your university' %>
    <% elsif current_user.profile.has_role?(:sponsor) %>
      <%= f.collection_select :organisation_id, Organisation.all, :id, :name, prompt: 'Select your organisation' %>
    <% elsif current_user.profile.has_any_role?(:grantor, :investor, :adviser, :innovation_consultant, :panel_member) %>  
      <%= f.collection_select :firm_id, Firm.all, :id, :name, prompt: 'Select your organisation' %>
    <% else %>
      <%=  current_user.profile.id %>
    <% end %>

I have the final else statement to prove that all of the above isn't working. When I try this, using the user with the role :student, should send me down the first route. It doesn't work. I get the user.profile.id instead.

Can anyone see what I have done wrong?

Prashant4224
  • 1,551
  • 1
  • 14
  • 21
Mel
  • 2,481
  • 26
  • 113
  • 273
  • `<%= puts current_user.profile.has_any_role?(:student, :educator, :researcher, :ktp, :faculty_manager, :ip_asset_manager) %>` what is the boolean value, please paste the result here. – Prashant4224 Oct 01 '15 at 07:26
  • `<%= puts current_user.profile.has_role? :student %>`, for this what is the o/p? – Prashant4224 Oct 01 '15 at 07:46
  • <%= puts current_user.profile.has_role? :student %> gives nothing. If i try without the 'puts' I get false. If I try using the profile id in the console, I get role = student – Mel Oct 01 '15 at 07:54

1 Answers1

2

You forgotten to add the require 'role_model' on top of model class

UPDATE

require 'rubygems'
require 'role_model'

class Profile < ActiveRecord::Base
  attr_accessor :roles_mask
  include RoleModel

  #reminder for roles (RoleModel): always add new roles to the end of this line - don't change the order
  roles_attribute :roles_mask

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

  # Other stuff goes here

end

In your case u.roles is empty:

> u.roles
=> #<RoleModel::Roles: {}> 

From console you need to assign the roles:

u = User.first
> u.roles
=> #<RoleModel::Roles: {}> 
> u.roles= [:admin]
=> [:admin] 
> u.roles
=> #<RoleModel::Roles: {:admin}> 
> u.roles << :manager
=> #<RoleModel::Roles: {:admin, :manager}> 
> u.roles
=> #<RoleModel::Roles: {:admin, :manager}> 
> u.has_any_role? :author, :manager
=> true 

For further Reference

Prashant4224
  • 1,551
  • 1
  • 14
  • 21
  • Hi, i didn't realise you have to require it as well as include it. I tried this suggestion, but it didn't work. I still get the same output as my original question – Mel Oct 01 '15 at 07:52