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
This deprecation warning appears because you passed ActionController::Base.view_paths
to ActionView::Base.new
as the first argument. This used to be okay but now an instance of ActionView::LookupContext
is expected. If you look at the most recent version of ActionView::Base#initialize
you'll see that where the message appears it calls ActionView::Base.build_lookup_context
using your first argument to ActionView::Base.new
. You can easily silence this warning by passing ActionView::LookupContext.new(ActionController::Base.view_paths)
or ActionView::Base.build_lookup_context(ActionController::Base.view_paths)
since that's what it ends up using anyways.
While ApplicationController.render
is helpful for rendering a view outside of a request sometimes you need an instance of ActionView::Base
by itself (in my case I use one as the view context for my presenter classes in their tests).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With