Rails.application.routes.default_url_options in backgound job does not exist in path helper method

361 Views Asked by At

Background job:

class ProcessJob < ActiveJob::Base
  queue_as :default

  def perform(room, object)
    payload = ApplicationController.render(
      partial: 'objects/object',
      locals: { object: object }
    )
    MyChannel.broadcast_to(room, payload: payload)
  end
end

Partial objects/_object.jbuilder view:

json.id object.id
json.name object.name
json.links do
  json.delete object_path(object)
end

When I try to render the file from the controller, the result is the following

{
  'id': 46,
  'name': 'Test',
  'links': {
    'delete': '/5/objects/46'
  }
}

Because it has account_id scope and default_url_options as follows

Rails.application.routes.draw do
  scope '(:account_id)' do
    resources :objects
  end
end

In controller:

class ApplicationController < ActionController::Base
  respond_to :html, :js, :json

  def default_url_options
    { account_id: current_account.id }
  end
end

Background jobs render gives exception for

ActionView::Template::Error: No route matches {:account_id=>#Object id: 1, name: "Test">, :action=>"delete", :controller=>"objects"}, missing required keys: [:id]

from app/views/objects/_objects.jbuilder:8:in block (4 levels) in _app_views_shared_partials_change_log__users_jbuilder___1004562150967553361_112564440' from app/views/objects/_object.jbuilder:7:inblock (3 levels) in _app_views_objects__object_jbuilder___1004562150967553361_112564440' from app/views/objects/_object.jbuilder:3:in block (2 levels) in _app_views_objects__object_jbuilder___1004562150967553361_112564440' from app/views/objects/_object.jbuilder:2:ineach' from app/views/objects/_object.jbuilder:2:in block in _app_views_objects__object_jbuilder___1004562150967553361_112564440' from app/views/objects/_object.jbuilder:1:in_app_views_objects__object_jbuilder___1004562150967553361_112564440'

0

There are 0 best solutions below