【发布时间】:2017-01-02 18:49:40
【问题描述】:
我正在尝试允许最终用户按关联记录过滤数据网格。数据网格显示活动列表:
activity.rb(记录列表)的模型
class Activity < ApplicationRecord
belongs_to :student
belongs_to :user
end
student.rb 的模型(我要过滤的内容)
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
这是 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>
...
还有我的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
我得到了带有学生名字的选择,但是当我点击它时,这是我得到的 js 错误:
GET http://localhost:3000/filter_students 404(未找到)
编辑路线如下:
Rails.application.routes.draw do
resources :activities
resources :students
end
【问题讨论】:
-
您可以将您正在使用的路线添加到问题中吗?
-
/filter_students的路线在哪里?现在,您的路由文件中只有两个条目,并且都没有配置为响应/filter_students处的GET请求
标签: javascript ruby-on-rails ajax ruby-on-rails-4