我将应用程序从 Rails 5.2 迁移到 Rails 6。只剩下一件事要做,但我不知道如何做。
我有这个弃用警告:
弃用警告:ActionView::Base 实例应使用查找上下文、赋值和控制器来构造。 (从 /Users/xxx/xxxx/app/models/stock.rb:42 处的 new 调用)
从这段代码:
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'
我不明白如何解决它。我去看了源代码中的折旧,但它并没有帮助我弄清楚我应该做什么,而且我并没有真正找到任何有关此“查找”的文档。
请问有人可以帮我理解这个折旧吗?
看起来您正在尝试在请求之外呈现视图。 Rails 过去添加了一项功能,简化了这一过程。现在您唯一需要做的就是打电话ApplicationController.render
与你的参数。在你的情况下,它应该看起来像这样:
ApplicationController.render(
template: 'admin/reports/logistics/stocks_by_age',
locals: { categories: categories, periods: periods } # maybe assigns: { ... }
handlers: [:axlsx],
formats: [:xlsx]
)
如果您有物流控制器,以下代码也应该有效:
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 https://blog.bigbinary.com/2016/01/08/rendering-views-outside-of-controllers-in-rails-5.html
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)