james
james

Reputation: 4047

Undefined route error (Rails)

I'm seeing the following error:

Error message: undefined local variable or method `call_alert_path' for #<RoadrunnerTwilioAlert:0x007f34401bbd10>

However, I feel like call_alert_path is properly defined in the routes. This is corroborated by the fact that my tests pass. The main difference between test mode & production is that in production, the method that calls call_alert_path is in an async job. Perhaps that's throwing it off... anyways, I just want to confirm with the community that call_alert_path is otherwise correctly defined and there's nothing wrong with the code as written.

Controller code:

# calls async job in production
if Rails.env == "production"
  RoadrunnerTwilioAlert.new.async.perform(params[:rentalrequest_id])
else
  @alert = twilio_client.account.calls.create(
    from: ENV["Twilio_Verified_Phone"],
    to: ENV["Roadrunner_Phone"],
    url: call_alert_path,
    method: 'post'
  )
  @request.update_attributes(twilio_alert: "call")
end

Async job code:

def perform(rentalrequest_id)
  @request = Request.find(id)
  @alert = twilio_client.account.calls.create(
    from: ENV["Twilio_Verified_Phone"],
    to: ENV["Roadrunner_Phone"],
    url: call_alert_path,
    method: 'post'
  )
  @request.update_attributes(twilio_alert: "call")
end

Route:

match '/twilio/call_alert', to: 'twilio#call_alert', via: :post, as: "call_alert"

Upvotes: 0

Views: 83

Answers (1)

infused
infused

Reputation: 24337

URL helpers are not available in a worker. Pass the URL as an argument to the worker instead:

def perform(rentalrequest_id, url)
  @request = Request.find(id)
  @alert = twilio_client.account.calls.create(
    from: ENV["Twilio_Verified_Phone"],
    to: ENV["Roadrunner_Phone"],
    url: url,
    method: 'post'
  )
  @request.update_attributes(twilio_alert: "call")
end

Upvotes: 2

Related Questions