My goal is to enable an end user to filter a data grid based on an associated record. The data grid currently displays a list of Activities:
The model for activity.rb (the list of records)
class Activity < ApplicationRecord
belongs_to :student
belongs_to :user
end
The model for student.rb (the filtering criteria)
class Student < ApplicationRecord
belongs_to :user
has_many :activities
validates_attachment_content_type :image, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"], :convert_options => "-auto-orient"
validates :firstname, :presence => true
end
Within the view in index.html.erb:
<heading>
<h1 class="activity">Activity</h1>
<div class="filter">
<%= collection_select :students, :ids, Student.where(user_id: current_user), :id, :firstname, {}, {:onchange => '$.get("/filter_students")'} %>
</div>
<div class="left">
<div class="timeblock">
<div class="label">Date</div>
<div class="value"><%= activity.created_at.strftime("%b %e") %></div>
</div>
<div class="student-name"><%= activity.student.try{|s| s.firstname} || "Deleted Student" %></div>
<%= activity.name %>
</div>
...
Additionally, in my activities_controller.rb:
def index
@activities = Activity.where(user_id: current_user).order('created_at DESC').limit(50)
end
def filter_students
@activities = Activity.find(params[:id])
respond_to do |format|
format.js { render 'student_activites', :formats => [:js] }
end
end
Even though I'm able to retrieve a dropdown with students' first names, when I select one, I encounter this JavaScript error:
GET http://localhost:3000/filter_students 404 (Not Found)
Edit Here are the routes defined:
Rails.application.routes.draw do
resources :activities
resources :students
end