【问题标题】:Rails Ajax - View Filtering by joined recordRails Ajax - 按加入记录查看过滤
【发布时间】: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


【解决方案1】:

应用正在检索的 URL 是:http://localhost:3000/filter_students。您需要更改为:http://localhost:3000/activities/filter_students,以指定您的操作的控制器。只需将控制器添加到 collection_select 上的 onchange 指令即可。
您还需要告诉Rails,您的ActivitiesController 中有一条路线filter_students,该路线应用于collection,例如过滤学生不适用于特定的activity,因此您需要输入你的routes.rb

resources :activities do
  collection do
    get :filter_students
  end
end
resources :students

【讨论】:

  • 现在收到GET http://localhost:3000/activities/filter_students 404 (Not Found)。我什至将其添加为带有索引标记的视图
【解决方案2】:

需要添加 filter_students 路由:

get 'filter_students', to: 'activities#filter_student'

【讨论】:

  • 呼叫仍然是 404,当我手动点击该页面时,我在 @activity = Activity.find(params[:id]) 中得到 Couldn't find Activity with 'id'=filtered_students
【解决方案3】:

根据您 activities_controllerfilter_students 方法的当前构造,我会这样做:

routes.rb

# filter_students is trying to find @activities based on params[:id]
## so you need to send params[:id] via the member do block of the activites resource below
resources :activities do
  # member rather than collection
  # member will allow you to pass params[:id] like you have in you controller
  member do
    get :filter_students
  end
end
resources :students

然后使用activity.id(看起来您已经在索引视图中使用activity)将活动ID添加到url

index.html.erb - 2017 年 1 月 3 日编辑

<%# your onchange url needs to look like this /activity/:id/filter_students %>
<%# ... to work with the routes you setup above and the logic in your controller %>
<%= collection_select :students, :ids, Student.where(user_id: current_user), :id, :firstname, {}, {:onchange => '$.get("/activity/<%= activity.id %>/filter_students")'} %>

【讨论】:

  • 我收到jquery.self-bd7ddd3….js?body=1:10255 GET http://localhost:3000/filter_students 404 (Not Found)
  • Window.location.href 应该在该 URL 中滑动 'activity/:id/'
  • 这是它在 DOM 中显示的内容 onchange="$.get("http://localhost:3000/filter_students")"
  • 您可以将您的活动 ID 添加到视图上该按钮的数据属性中吗?然后它可以作为 JavaScript 变量访问,用于您的更改 URL
  • 我该怎么做?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多