gpt4 book ai didi

ruby-on-rails - 从演示者/模块访问 Rails 助手?

转载 作者:行者123 更新时间:2023-12-04 06:17:56 24 4
gpt4 key购买 nike

关于从模型中访问 Rails 助手有很多问题,所有这些问题都正确地表明答案是不这样做。这不是那个问题。

我有一些相当复杂的 Controller 和 View 代码,我想将它们抽象为演示者类。但该类不是 ApplicationController 的后代。我怎样才能访问,比如说,设计的 current_user?

最佳答案

目前似乎没有“官方正确”的方式来做到这一点。两种可能:

  • 这很 hacky,但是您可以将当前 Controller 存储在 ApplicationController 中,并在您的演示者中引用它以获取帮助程序:
    class ApplicationController < ActionController::Base
    prepend_before_filter { @@current_controller = self }
    end

    class YourPresenter
    def current_user
    ApplicationController.current_controller.current_user
    end
    end
  • Jeff Casimir 正在开发一个名为 draper 的出色装饰器/演示器 gem ,它封装了整个想法:

  • https://github.com/jcasimir/draper

    关于ruby-on-rails - 从演示者/模块访问 Rails 助手?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6998830/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com