0

I am using Kaminari Paginate to traverse through users and give them an evaluation. However, certain users can only evaluate certain users, not all of them, but I am confused on how to go about this. I tried creating an array of the users I want to display, but it did not work correctly since I am very new with Ruby. I understand that

<% @users.each do |user| %>

is what is traversing through the entire list, but I am not sure how to edit it to my needs. For example, I have a role attribute for each user. I would like users with each role to only evaluate other users with the same role. I have tried doing something like

<% @users.each do |user| if @user.role == 1 %>

but I know that that exact code verbatum won't work the way I'd like, but that's an example of what I want to do if it's possible. In summary, I would like to choose who is in the paginated list. Below is my code that I am trying to do. Each user has a form.

<h2>Back of House Evaluation</h2>
<br> </br>

<% @users.each do |user| %>

<h4> <%= user.name  %> </h4>
<h3> Do you have HEART?</h3>


<%= form_for(user) do |f| %>
<% begin %>


       <div class="form-group">
        <%= f.label :Hunger_for_wisdom %></label
        <div class="col-xs-10">
        <div class="form-inline">
        <div class="form-group">
             <%= f.radio_button :current_one, 1 %>
             <%= f.label :_1 %>
        </div>
        <div class="form-group">
             <%= f.radio_button :current_one, 2 %>
             <%= f.label :_2 %>
        </div>
        <div class="form-group">
             <%= f.radio_button :current_one, 3 %>
             <%= f.label :_3 %>
        </div>
        <div class="form-group">
             <%= f.radio_button :current_one, 4 %>
             <%= f.label :_4 %>
        </div>
        <div class="form-group">
             <%= f.radio_button :current_one, 5 %>
             <%= f.label :_5 %>
        </div>
    </div>
</div>

     <div class="col-md-3 col-md-offset-4">
  <%= f.submit "Submit Score", class: "btn btn-default" %>



    <% rescue ActionController::RedirectBackError %>


</div>
</div>
<% end %>

<%= paginate @users %>

<% end %>

<% end %>

Thank you so much in advance!!

user3376654
  • 67
  • 1
  • 9

2 Answers2

2

One way to accomplish this is by using the method reject! for your user list. Replace:

<% @users.each do |user| %>

by:

<% @users.reject!{|u| u.role != 1}.each do |user| %>

But I'd prefer to filter out unwanted users from the list in the controller, not in the view. So in your controller you could call something like:

@users = User.where(:role => role_id).page(params[:page])

where role_id is the id of the role (1 for your example), and the params[:page] is the page number you want to load from Kaminari.

Hope this helps you out.

evedovelli
  • 2,115
  • 28
  • 28
0

Assuming that @user is used to filter the visible list of users, you can do something like this with your collection:

<% @users.select{|u| u.role == @user.role}.each do |user| %>
  # your logic/content here
<% end %>

This way, only the users with the role of @user's role are iterated through.

Hope it helps!

Zoran
  • 4,196
  • 2
  • 22
  • 33