首页 > 解决方案 > 自动过滤索引页面上的帐户

问题描述

在我Account的 s 索引页面上,我在页面顶部有一个表单,其中有几个<select>允许过滤的 s。在最初的 get 请求中,我想按 current 过滤Staffer,同时保持选择 any 的能力Staffer。正如下面编写的代码,Staffer在页面加载时选择当前,但这不会过滤结果。

  <%= bootstrap_form_with url: accounts_path, scope: :filters, remote: true, method: :get, data: {controller: "filterable"} do |f| %>
    <div class="row">
      <div class="col-3">
        <%= f.select :staffer_id, options_from_collection_for_select(Staffer.ordered, :id, :full_name, selected: current_staffer.id), {label: "Responsible Staffer", prompt: "Anyone"}, data: {action: "change->filterable#apply"} %>
      </div>

标签: ruby-on-rails

解决方案


在您的控制器上,您需要在显示页面之前进行过滤

class AccountsController < ApplicationController
  def index
    staffer_id = params[:staffer_id].presence || current_staffer.id
    @accounts = Account.where(staffer_id: staffer_id)
  end
end

推荐阅读