Rails URL helpers

Topics: Ruby on Rails

A background job can include Rails.application.routes.url_helpers and pass a host to build an absolute URL for a record.

Using URL helpers outside views

Instead of building the URL yourself, you can rely on the URL helpers which will always be based on your current routes:

class RequestUserCallBackJob < ApplicationJob
  include Rails.application.routes.url_helpers

  def perform(user)
    Net::HTTP.post(
      "http://userinfoapi.com/",
      body: {callback_to: user_url(user, host: "myapp.com")})
  end
end