I am trying to make an app in Rails 4. I use devise for authentication and pundit for authorisations. I want to use Rolify for role permissions.
I posted this question and got some really helpful starting out advice.Defining Roles with Rolify
I have made a controller and views because I want roles to be assigned by other users and subject to criteria (such as periods of tenure e.g.: a student will hold a role as student until the end of their course date).
I also want to be able to add new roles as the app expands.
I have used seeds.rb to create the initial roles. I have not limited the use of roles to the admin yet - but will make a pundit policy to do this once I have this set up and working.
I don't have much experience with HABTM relationships and am getting stuck in trying to understand how they work.
I want to make a show page for my roles - which shows each user who has been assigned a role.
I have:
<div class="formheader"><td><%= @role.name %></td></div>
<div class="row">
<div class="table-responsive" style="margin-left:30px; margin-top:15px">
<table class="table table-bordered">
<tr>
<td><span class="intpolth">User</span></td>
<td><span class="intpolth">Commenced</span></td>
<td><span class="intpolth">Terminated</span></td>
</tr>
<% @roles.user.each do |role| %>
<tr>
<td><%= user.formal_name %></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<% end %>
At this point, I think my error might be more to do with how to express HABTM joins than how to work with Rolify - but can anyone see what I've done wrong? The purpose of this table is to make a list of each user name that has been allocated the relevant role.
I get this error message when I try this:
undefined method `user' for nil:NilClass
I have noticed that my rolify migrations made a join table called 'users_roles'. I understood the convention to be the table names were set out in alphabetical order and that the first table name was singular rather than plural. Could this have something to do with my issue?
All I have at this point is a method in my user model that says:
after_create :add_default_role
def add_default_role
add_role(:guest) if self.roles.blank?
end
I have made roles in my seeds.rb which include :guest.
My console shows I have that role:
Role Load (4.2ms) SELECT "roles".* FROM "roles" WHERE "roles"."name" = $1 [["name", "guest"]]
=> #<ActiveRecord::Relation [#<Role id: 23, name: "guest", resource_id: nil, resource_type: nil, created_at: "2016-01-16 09:46:16", updated_at: "2016-01-16 09:46:16">]>
Im just getting started and struggling to understand how this works.
I should say, the generator for this gem did make a role model, but it has nothing in it. Do I need to add anything to the role model to get this working?
class Role < ActiveRecord::Base
end