Mauricio
Mauricio

Reputation: 127

How do I show Users that are "Pending" to join Group?

What I want to do is show the Users that are "pending" to join a Group. I have 3 models: Users, Membership, & Groups. I have enabled Users to be able to request to join a Group. When the request is made there is a attribute in the Membership model called "state" which I made the default value to be "pending". Once the Group Admin clicks accept then the User's state is changed to "active". How do I show only the Users that are in the "pending" state?

I am able to show all Users (both "pending" & "active") who request to join the Group by adding code "@members = @group.users" in the Group controller. When I wanted to show only the "pending" Users I created a method in model Group called "pending_members". This didn't work.

Currently I get an error message that says: "ActionView::MissingTemplate in Groups#show". The error message also highlights the code "<%= render @members %>". [When I use the code @members = @group.users then the webpage does show both "pending" & "active" Users]


Here are each of the Model attributes:

create_table "groups", force: :cascade do |t|
  t.string   "name"
end

create_table "memberships", force: :cascade do |t|
  t.integer  "user_id"
  t.integer  "group_id"
  t.string   "state",   default: "pending"
end

create_table "users", force: :cascade do |t|
  t.string   "email"
  t.string   "first_name"
  t.string   "last_name"
end

Here is the code in each respective model:

class Membership < ActiveRecord::Base
  belongs_to :user
  belongs_to :group
end

class User < ActiveRecord::Base
  has_many :memberships, dependent: :destroy
  has_many :groups, through: :memberships
end

class Group < ActiveRecord::Base
  has_many :memberships, dependent: :destroy
  has_many :users, through: :memberships
end

Here is the code in the controller for Groups:

def show
    @group = Group.find(params[:id])
    @members = @group.pending_members
end

Here is the code in the model for Groups:

def pending_members
  Membership.where(state: "pending")
end

Here is the code in the view for Groups/show.html.erb:

<div class="container">
    <div class="row">
        <%= render @members %>
    </div>
</div>

Upvotes: 1

Views: 249

Answers (1)

Toby 1 Kenobi
Toby 1 Kenobi

Reputation: 5037

If you want to show users in the pending state, then you pending_members method should return a collection of users. As it is it returns a collection of memberships.

You could write it like this:

def pending_members
  User.includes(:memberships).where("memberships.state" => "pending)
end

then for your view to work you could put this in

<%= render partial: 'member', collection: @group.pending_members %>

Then you need to have in the app/views/groups directory the partial _member.html.erb which might look something like this:

<div id="user-<%= member.id %>">
  <p>name: <%= member.first_name %> <%= member.last_name %></p>
  <p>email: <%= member.email %></p>
</div>

Upvotes: 1

Related Questions