dimanche 11 février 2018

Fetch id of selected option in dropdown in controller

I have a model named 'Assessment':

class Assessment < ApplicationRecord
  has_many :assessment_students
  has_many :students, through: :assessment_students
end

Join table is:

class AssessmentStudent < ApplicationRecord
  belongs_to :student
  belongs_to :assessment
end

There is another model:

class Classroom < ApplicationRecord
 has_many :classroom_students
 has_many :students, through: :classroom_students
 has_many :assessments
end

In show,html.erb of classrooms, I have a dropdown which shows all assessments (generated from assessment table).

Code is:

<%= collection_select(:assessment :assessment_id, Assessment.all, :id, :assessment_name , :prompt => true) %>

Requirement of the project is: Based on the assessment chosen by the user in the show.html.erb page, we have to show all students details like name etc assigned to that particular assessment. I have stored this data in join table 'AssessmentStudent '. However, I am not sure how to pass id from the above collection_select to classroom controller. I have below code:

show.html.erb:

<%= collection_select(:assessment :assessment_id, Assessment.all, :id, :assessment_name , :prompt => true) %>

<div id="divResult">
 <% @assessmentstudents1.each do |t| %>
      <% t.assessment_students.each do |record| %>
        <%= record.student_id %>
     <% end %>  
   <% end %>  
</div>

classroom controller:

def show
   @assessmentstudents1 = Assessment.find(params[:assessment][:assessment_id]).preload(:assessment_students)
end

def classroom_params
  params.require(:classroom).permit(:classroom_name, :classroom_year, :customer_id, :classroom_student, :student_ids => [])
  params.require(:assessment).permit(:assessment_id)
end

Aucun commentaire:

Enregistrer un commentaire