20

I'm receiving a deprecation warning when running rails test. That warning being below. Any help is appreciated in identifying what I'm doing incorrectly.

(Edit: Side note, the render MUST break and return from the current controller call. I attempted to use ApplicationController.render(...) in place of the current render call, but that did not return from the controller call and I was receiving errors/warnings of :no_content rendered.)

Warning:

DEPRECATION WARNING: Rendering actions with '.' in the name is deprecated: actions/action_success.json (called from update at /<path>/app/controllers/table_base_controller.rb:39)

The code throwing the warning is specifically this call to render within a controller:

render('/actions/action_success.json', locals: {
  view: action.lookup_view('default'),
  action: action,
  area: current_area,
  account: current_account
}) 

I've tried taking off the .json as directed (also tried adding template: <path>, tried file: <path>), however, I receive this error in the test console:

Error:
TableControllerTest#test_Admin_should_update_via_loan_table:
ActionView::MissingTemplate: Missing template actions/action_success with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :jbuilder]}. Searched in:
  * "/<path>/app/views"

    app/controllers/table_base_controller.rb:39:in `update'
    app/controllers/application_controller.rb:79:in `with_account'
    test/controllers/table_controller_test.rb:14:in `block in <class:TableControllerTest>'

The file in question (path: app/views/actions/action_success.json.jbuilder):

# frozen_string_literal: true

json.status 'success'
json.status_code 200
json.messages action.messages

if view
  json.result do
    json.partial! view.to_s, result: action.result, locals: { area: area }
  end
else
  json.result action.result
end
B-M
  • 1,231
  • 1
  • 19
  • 41
  • 3
    Your request hitting the action isn't requesting a json response so it's attempting to render HTML – CWitty Aug 18 '21 at 18:31

1 Answers1

22

Deprecating partial names that include a . was done to prevent ambiguity in parsing the partial name. We should explicitly state formats instead of including them in the partial name we pass to render.

Without the format in the string you pass in, you need to ensure the formats the render is expecting includes the format you're using, in this case json, which is not one of the default formats.

You can send it in as an option (and make the partial name an option as well) like this:

render(
  partial: '/actions/action_success',
  formats: [:json],
  locals: {
    view: action.lookup_view('default'),
    action: action,
    area: current_area,
    account: current_account
  }
) 
L0ne
  • 470
  • 3
  • 10