ruby-on-rails - 自动过滤索引页面上的帐户
问题描述
在我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>
解决方案
在您的控制器上,您需要在显示页面之前进行过滤
class AccountsController < ApplicationController
def index
staffer_id = params[:staffer_id].presence || current_staffer.id
@accounts = Account.where(staffer_id: staffer_id)
end
end
推荐阅读
- angular - 标头未赋值
- android - com.crashlytics.android.Crashlytics 不能存储在 io.fabric.sdk.android.Kit[] 类型的数组中
- html - 将元素引用从 DOM 传回给视图模型
- python - 将 Python 中带有动态片段的文本打印到终端
- ios - 如何在我的课堂上使用协议和委托?
- docker - docker run --init 的 docker-compose 等价物是什么?
- oracle12c - Oracle 12c IMPDP 错误
- php - 如何使用 PHP 将值插入 FK 列
- docker - docker-compose 构建冻结。没有错误
- redirect - weblogic & OHS:如何将一个 url 转发到另一台服务器