Ruby / Rails Wait for redirect_to to resolve and do something depending on response

250 views Asked by At

I've got a redirect_to in a rails app that hits an endpoint to send a video to Zencoder to be encoded into a few different formats.

Just after I've got a redirect_to that redirect to a view. What's happening is the redirect to the view happens too quickly, so when the view makes the first request for the Zencoder urls - it throws 404s. On refresh the requests are 200

I'd like to either:

  1. Delay the redirect momentarily so that the 404's don't occcur (simpler)

  2. Listen to the Zencoder redirect and only redirect to the view after getting a response from Zencoder

Here's the relevant code:

  def edit
    @director = Director.find(params[:id])
    @director_videos = Video.where(:director_id => @director.id)
    if params[:bucket].present?
      # process video
      key = params[:key]
      video = Video.new
      video.director_id = @director.id

      video.video_url = "https://"+Settings.S3_BUCKET + ".s3.amazonaws.com/" + URI.unescape(key)
      video.save    
      video.process
      redirect_to '/encode_progress' and return
    end
    @video_uploader = Video.new.video
    @video = Video.new
    redirect = "http://"+Settings.DOMAIN+"/directors/"[email protected]_s+"/edit"
    @video_uploader.success_action_redirect = redirect
  end
0

There are 0 answers