gpt4 book ai didi

ruby-on-rails - 弃用警告:ActionView::Base 实例应该使用查找上下文、赋值和 Controller 构建

转载 作者:行者123 更新时间:2023-12-04 12:08:21 25 4
gpt4 key购买 nike

我将应用程序从 rails 5.2 迁移到 rails 6。只剩下一件事要做,但我不知道如何做。
我有这个折旧警告:

DEPRECATION WARNING: ActionView::Base instances should be constructed with a lookup context, assignments, and a controller. (called from new at /Users/xxx/xxxx/app/models/stock.rb:42)


从这个代码:
view = ActionView::Base.with_empty_template_cache.new(
ActionController::Base.view_paths,
categories: categories,
periods: periods
)

result = view.render formats: [:xlsx],
handlers: [:axlsx],
template: 'admin/reports/logistics/stocks_by_age'
我不明白如何解决它。我去查看源代码中的折旧,但它没有帮助我弄清楚我应该做什么,而且我没有真正找到任何有关此“查找”的文档。
请问有人可以帮我理解这种折旧吗?

最佳答案

看起来您正在尝试在请求之外呈现 View 。 Rails 过去添加了一个功能,简化了这一点。现在您唯一需要做的就是拨打 ApplicationController.render用你的参数。在你的情况下,它应该是这样的:

ApplicationController.render(
template: 'admin/reports/logistics/stocks_by_age',
locals: { categories: categories, periods: periods } # maybe assigns: { ... }
handlers: [:axlsx],
formats: [:xlsx]
)

如果您有物流 Controller ,以下代码也应该适用:

Admin::Reports::LogisticsController.render(:stocks_by_age, ...other params same as above..., handlers: [:axlsx], formats: [:xlsx])

有关如何执行此操作的更好说明,请参阅以下文章。 https://blog.bigbinary.com/2016/01/08/rendering-views-outside-of-controllers-in-rails-5.html

关于ruby-on-rails - 弃用警告:ActionView::Base 实例应该使用查找上下文、赋值和 Controller 构建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59773680/

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