Categories
Mastering Development

Ruby on Rails trying to update a boolean in a user table while logged in as another entity

I am very novice at Ruby and Ruby on Rails.
I am trying to update a web application that has signed up volunteers that require approval before they can access full website functionality. I added a boolean field to the volunteers database model called :approved that defaults to false when a volunteer signs up, and requires a logged in administrator to update it to true.
I created an administrator class in my project that can view a list of pending volunteers from the controller:

def view_pending_volunteers
    @navbar = true
    @volunteers = Volunteer.where(approved: false)
end

In the administrator view I want to use checkboxes associated with volunteer, which when submitted will allow the administrator to update the :approved field in the volunteer table.

I have several issues that are not clear to accomplish this task:

  1. In my administrator model I use has_many :volunteers with a migration that put the volunteer_id as a foreign key in the administrator database table. Is that sufficient to accomplish the task at hand, or do I need to create a join table for :approved? I can’t have volunteers belong_to :administrators because they would not be able to sign up on their own, they need to be created by administrators.
  2. I am not sure exactly how to configure the code for checkbox helpers in my administrator view. I am using form_with but I am sure my code is not correct. I would like to pass an array of parameters for each check_box associated with the :approved field for the list of pending volunteers back to the controller.
<p><b style="color:blue;font-size:24px;">
<%= form_with(model: @volunteer, local: true) do |f| %>
   <% @volunteers.each do |v| %>
   <li>
     Volunteer: <%= x.first_name%> <%= x.last_name%> &emsp;Age: <%= x.age%> &emsp;Description: <%= x.description%>&emsp;
     <%= f.check_box (@volunteers, v.approved, options = {}, checked_value = "1", unchecked_value = "0")%>
                         
    </li>
    <% end %>
 <%= f.submit "Approve" %>
<% end %>
</b></p>
  1. I am not exactly sure how to handle the array of checkbox values that get returned to the administrator controller in order to update the volunteer database table. Do I need to create a hash in the view (how to do that?) and then loop through the hash to update_attribute(:approved, true) for each checked volunteer? I have created an update_pending_volunteers method in the administrator controller for the POST operation, but am unclear on the code that should be there because I am unsure the checkbox approach.

Thanks in advance for your patience with a newbie. This seems like such a simple thing to do but I am not sure of the proper approach. I have spent quite a bit of time reviewing APIs and videos and stack overflow articles but cannot seem to piece together information that will give me confidence in a particular approach to do this correctly. Again it seems like such a simple thing to accomplish but has become a source of frustration.

Leave a Reply

Your email address will not be published. Required fields are marked *