ruby-on-rails - Ruby on Rails / Ransack:过滤属于当前用户的所有对象
问题描述
假设我有一个要使用 Ransack 过滤的帖子列表。我的视图模板看起来像这样(我使用 simple_form):
帖子.haml:
= search_form_for @q, class: "form" do |f|
# this works if enabled but sure enough fails if disabled
# = f.input :user_id_eq, as: :boolean, value: current_user.id, label: "Show only my posts"
= f.input :title_cont, required: false
%button{type: "submit"} Search
用户.rb
class User < ApplicationRecord
has_many :posts
end
post.rb
class Post < ApplicationRecord
belongs_to :user
end
[编辑:添加模型和半工作解决方案]
现在我想添加一个复选框来过滤当前用户的帖子,如下所示:[ ] Show only my posts
.
我有一个关联:用户 has_many posts / post belongs_to user
如何在我的搜查表单中添加一个复选框以过滤与当前用户关联的所有帖子?
解决方案
推荐阅读
- python - 通过 .txt 文件解析以创建制表符分隔的输出文件
- java - 将表示数组的 JSON 转换为 String[]
- itext - 为什么 itext5 会生成通过 Acrobat Reader 看不到的文本?
- django - MultipleObjectsReturned 在复杂的查询集中,而结果应该是唯一的
- vue.js - vue js中“v-bind:value”和“v-bind:key”的区别
- node.js - 与路由中的回调作斗争 - NodeJS
- python - Python 3 Tkinter - 尝试通过一个按钮阻止多个窗口打开
- r - 弹出窗口中的传单 html 小部件(迷你图)
- docker - Kubelet 运行失败:无法创建证书签名请求:未经授权
- php - 哪些 PHP 错误级别会停止执行?