将过滤器添加到表Ruby on Rails

时间:2017-11-19 15:03:06

标签: ruby-on-rails ruby filter

我按照本教程编写了一个带有搜索框http://railscasts.com/episodes/240-search-sort-paginate-with-ajax的可排序表格。 这很好,但现在我需要在表中添加一个过滤器。单击字段值时,例如作者,该表必须仅显示该作者的记录。 我有以下内容:

class IssuesController < ApplicationController
  before_action :set_issue, only: [:show, :edit, :update, :destroy, :do_vote, :do_watch]
  before_action :current_user, only: [:show, :edit, :update, :destroy, :index, :new, :create, :post_comment, :do_vote, :do_watch]
  helper_method :sort_column, :sort_direction

  def index
    @issues = Issue.search(params[:search]).order(sort_column + " " + sort_direction).paginate(:page => params[:page], :per_page => 5)
  end
...

如何在那里添加我的过滤器?

1 个答案:

答案 0 :(得分:0)

只需在问题查询中添加where(author: params[:author])

@issues = Issue
  .search(params[:search])
  .where(author: params[:author])
  .order(sort_column + " " + sort_direction)
  .paginate(page: params[:page], per_page: 5)