访问模型中设备的 current_user

发布于 2024-10-31 00:22:28 字数 119 浏览 1 评论 0原文

如何访问模型内​​部的 Devises 'current_user' 对象?我想在创建某些内容时自动将当前用户 ID 附加到记录中。我知道我可以在控制器内部手动设置 user_id,但我觉得如果由模型处理它会更容易/更干净。

How do I go about accessing Devises 'current_user' object inside of a model? I want to automatically append the current users id to a record when they create something. I know that I can manually set the user_id inside of the controller but I feel it would be easier/cleaner if it was handled by the model.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

幼儿园老大 2024-11-07 00:22:29

本质上,这种逻辑不属于模型,因此您最好通过模型中的函数或在创建时传递 current_user这个答案总结了这一点向上。

我知道,我知道,这不是你想要的答案。不幸的是,它对您来说可能看起来更干净,但它增加了您真正不想要的模型的开销。

Essentially, this kind of logic doesn't belong in a Model, so you're best to pass current_user in through a function in the model or on creation. This answer sums it up.

I know, I know, that's not the answer you wanted. Unfortunately, it may be cleaner looking to you, but it adds overhead to your model that you really don't want.

梅窗月明清似水 2024-11-07 00:22:29

许多流行的gem需要访问模型中的current_user,例如paper_trail< /a>.

他们的方法从捕获 current_user 的控制器开始。

class ApplicationController
  before_action :set_paper_trail_whodunnit
end

哪个触发器:

def set_paper_trail_whodunnit
  if ::PaperTrail.request.enabled?
    ::PaperTrail.request.whodunnit = user_for_paper_trail
  end
end

以及

def user_for_paper_trail
  return unless defined?(current_user)
  current_user.try(:id) || current_user
end

从控制器保存 current_user.id 后,触发审核操作(例如创建)。

class Widget < ActiveRecord::Base
  has_paper_trail
end

最终触发以下事件。

module PaperTrail
  module Events    
    class Create < Base
      def data
        data = {
          item: @record,
          event: @record.paper_trail_event || "create",
          whodunnit: PaperTrail.request.whodunnit
        },
        # .. more create code here 
    end
  end
end

总之,一种方法如下。

  1. 在控制器中使用 before_action 保存 current_user
  2. current_user 存储在模型中的 paper_trail 中稍后可以访问的位置code> 的情况是 PaperTrail.request.whudunnit
  3. 访问模型内​​的商店

Many popular gems need to access the current_user in the model such as paper_trail.

Their approach starts in the controller which captures the current_user.

class ApplicationController
  before_action :set_paper_trail_whodunnit
end

Which triggers:

def set_paper_trail_whodunnit
  if ::PaperTrail.request.enabled?
    ::PaperTrail.request.whodunnit = user_for_paper_trail
  end
end

and

def user_for_paper_trail
  return unless defined?(current_user)
  current_user.try(:id) || current_user
end

After the current_user.id is saved from the controller, and an auditing action is triggered such as create.

class Widget < ActiveRecord::Base
  has_paper_trail
end

Which eventually triggers the following event.

module PaperTrail
  module Events    
    class Create < Base
      def data
        data = {
          item: @record,
          event: @record.paper_trail_event || "create",
          whodunnit: PaperTrail.request.whodunnit
        },
        # .. more create code here 
    end
  end
end

In summary, an approach would be the following.

  1. Save the current_user using a before_action in a controller
  2. Store the current_user somewhere that can be accessed later in the model, in paper_trail's case that is PaperTrail.request.whudunnit
  3. Access your store inside the model
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文