I'm trying to make an attendance sheet for my fishing club meetings which shows all the active members and allows you to put a checkbox next to their name, to record if they attended the meeting for a specific tournament. I created a "Meeting" scaffold and within the _form, I list out all the active members and allow the user to put a checkbox if the member attended the meeting for the selected tournament. However, I am having issues passing an array of hashes to my meetings_controller, and am quite confused.
I read a bunch of articles, but baselined my design off of this one: Submit array of hashes with rails
The article does not show what is in the create method, so I have this...
meetings_controller:
def create
# puts " OUTPUT TEXT: #{params} "
@meeting = params[:meetings][:meetings]
@meeting.each do |m|
#If there is no attendance key, its nil. Make it false
# if !m[:meeting].has_key?("attendance")
# m[:meeting].attendance = false
# end
puts "OUTPUT TEXT: #{m[:meeting]}" # OUTPUT TEXT: {"member_id"=>"1", "tournament_id"=>"2", "attendance"=>"1"}
@meeting = Meeting.new(member_id: m[:meeting][:member_id], tournament_id: m[:meeting][:tournament_id], attendance: m[:meeting][:attendance])
end
respond_to do |format|
if @meeting.save
format.html { redirect_to @meeting, notice: "Meeting was successfully created." }
format.json { render :show, status: :created, location: @meeting }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @meeting.errors, status: :unprocessable_entity }
end
end
end
_form inputs: (based on article linked above)
<% Member.where(active: true).each do |member| %>
<tr>
<td> <%= member.full_name %> </td>
<input multiple="multiple" value=<%=member.id %> type="hidden" name="meetings[meetings][]meeting[member_id]" />
<input multiple="multiple" value=<%=@tournament.id %> type="hidden" name="meetings[meetings][]meeting[tournament_id]" />
<td><input type="checkbox" value="1" name="meetings[meetings][]meeting[attendance]" /></td>
</tr>
<% end %>
When I click to submit the form its just taking me to the show page where only this is shown on a blank page...
{"controller"=>"meetings", "action"=>"show", "id"=>"18"}
Even when I have a redirect line in the show method
def show
redirect_to meetings_path
end
I've spent a lot of time reading, and doing trial and error attempts to get this to work. I am hoping the stackoverflow gods can help.