I migrate an application from rails 5.2 to rails 6. There is only have one thing left to do but I don't know how.
I have this depreciation warning:
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)
from this code:
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'
I don't understand how to fix it. I went to see the depreciation in the source code, but it didn't help me figure out what I should do, and I didn't really find any documentation for this 'lookup'.
Please, could someone help me to understand this depreciation?
It looks like you are trying to render view outside of the request. Rails added a feature in the past, that simplified this. Now only thing you need to do is to call ApplicationController.render
with your params. In your case it should look something like this:
ApplicationController.render(
template: 'admin/reports/logistics/stocks_by_age',
locals: { categories: categories, periods: periods } # maybe assigns: { ... }
handlers: [:axlsx],
formats: [:xlsx]
)
Also following code should work as well if you have logistics controller:
Admin::Reports::LogisticsController.render(:stocks_by_age, ...other params same as above..., handlers: [:axlsx], formats: [:xlsx])
See the following article for better description of how to do it. https://blog.bigbinary.com/2016/01/08/rendering-views-outside-of-controllers-in-rails-5.html