简体   繁体   English

如何限制简单表单关联以仅显示当前用户的值

[英]How to limit simple form association to show only current user's values

I'm building an expense tracker with Ruby on Rails 7. I used pundit gem to authorize each user to access only their own data.我正在 Rails 7 上使用 Ruby 构建一个费用跟踪器。我使用 pundit gem 授权每个用户只能访问他们自己的数据。 But still, when I try to add a new transaction, it shows all bank accounts not only current users' accounts.但是,当我尝试添加新交易时,它会显示所有银行账户,而不仅仅是当前用户的账户。

This is how I defined the relationship between models:这就是我定义模型之间关系的方式:

class User < ApplicationRecord
  has_many :accounts, dependent: :destroy
  has_many :categories, dependent: :destroy
  has_many :transactions, through: :accounts
end

class Account < ApplicationRecord
  belongs_to :user
  has_many :transactions, dependent: :destroy

  enum :acc_type, [:Checking, :Savings]
  enum :bank_name, [:Westpac]
end

class Transaction < ApplicationRecord
  belongs_to :account
  belongs_to :category

  enum :tx_type, [:Debit, :Credit]

  scope :ordered, -> { order(date: :desc) }
end

This is the simple form for the transaction#new:这是事务#new 的简单形式:

<%= simple_form_for transaction do |f| %>
  <% if transaction.errors.any? %>
    <div class="error-message alert alert-danger alert-dismissible fade show">
      <%= transaction.errors.full_messages.to_sentence.capitalize %>
    </div>
  <% end %>
  <%= f.input :date, as: :date, html5: true %>
  <%= f.input :description %>
  <%= f.input :tx_type, collection: Transaction.tx_types.keys, as: :radio_buttons, item_wrapper_class: 'form-check-inline' %>
  <%= f.input :tx_amount %>
  <%= f.association :account, label_method: :acc_name, value_method: :id, prompt: "Choose account" %>
  <%= f.association :category, prompt: "Choose category" %>
  <%= f.input :notes %>
  <%= f.button :submit, class: "mt-3 btn btn-primary" %>
<% end %>

I just want to figure out how to declare this association in the right way to get a list of current_user's accounts.我只是想弄清楚如何以正确的方式声明此关联以获取 current_user 的帐户列表。

<%= f.association :account, label_method: :acc_name, value_method: :id, prompt: "Choose account" %>

Because, this one gives me all the accounts, not only the ones added by the current user.因为,这个给了我所有的账户,而不仅仅是当前用户添加的账户。

This is the GitHub repo if it is helpful: https://github.com/jkvithanage/finance-manager如果有帮助,这是 GitHub 回购协议: https://github.com/jkvithanage/finance-manager

Specify the collection as given in doc指定文档中给出的集合

https://github.com/heartcombo/simple_form#associationshttps://github.com/heartcombo/simple_form#associations

f.association :account, collection: current_user.accounts, prompt: "Choose a Account"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 f. 与 simple_form 中的 current_user 关联 - f.association with current_user in simple_form 在简单表单关联中指定值 - specify values in Simple Form Association 简单形式关联输入:按条件限制选项 - simple form association input: limit options by condition 仅当用户与当前用户联系时才显示他们的个人资料 - Only show a user's profile if they are contacts with current user 如何将引导图标添加到 simple_form 的 link_to_remove_association 和 link_to_add_association - how to add an bootstrap icon to simple_form's link_to_remove_association and link_to_add_association 如何过滤 rails 表单以仅显示没有标签的对象,并且由当前用户显示? - How can a rails form be filtered to show only objects without tags, and by a current user? 当用户在RoR中提交数据时,如何创建简单表单并显示输入的值 - How do i create a simple form and show the entered values when user submit data in RoR 自定义pg_search以仅显示当前用户的内容 - Customize pg_search to only show current user's content 如何将我的关联ID插入简单的表单中 - How to insert my association id into simple form for 如何在simple_form中处理嵌套关联中的选定值? - How do I handle selected values in a nested association in simple_form?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM