I'm working on an app in Rails that has teacher and student users. I want to list all the users that belong to a teacher and then let the teacher select a student to send a message to. I have a message model that has :content and :user_id for params. I want to set the :user_id based on the student selected. Here is my code:
<% @students = Student.all %>
<% @students.each do |student| %>
<% if student[:teacher_id] == @current_user.id %>
<%= radio_button 'student', 'id', student.id %>
<%= student.name %>
<% @current_student = Student.find(student.id) %>
<% end %>
<% end %>
<%= form_for Message.new do |f| %>
<%= f.text_area :content, class: 'messageTextarea' %> <br>
<%= f.hidden_field :user_id, :value => @current_student.id %>
<%= f.submit %>
<% end %>
So, I am looping through all students and printing those that have the same ID as the current user (the teacher) with a radio button. The radio button's value is the student's id. Then I want to pass that value to the :user_id of the message so it will then be retrievable for the student's view.
Currently, this code always passes in the value of the last student listed rather than the student selected. How can I change
<% @current_student = Student.find(student.id) %>
to find the student selected rather than the last one?
I did test this code using
<%= f.hidden_field :user_id, :value => @current_user.id %>
and that did work, but I don't want the current_user's id, of course. Thanks for any help!