I am attempting to get a variable called @s3_direct_post from my controller into Application.js.erb; here is my controller action:

def new
  unix_ts = Time.now().to_i.to_s
  @s3_direct_post = S3_BUCKET.presigned_post(key: "#{unix_ts}_${filename}",
                                             success_action_status: 201,
                                             acl: :public_read)
  @video = Video.new({title: "Title with hashtags"})

Application.js.erb has two lines that reference @s3_direct_post:

url: '<%= @s3_direct_post.url %>',


formData: '<%= @s3_direct_post.fields.to_json.html_safe %>'

But these are not being processed by erb. I get a 400 Bad Request error with the following requested URL message:


Any suggestions on how to correctly transfer this value into Application.js.erb so it populates with the value of the instance variable?

P.S. If I remove the quotes from each of these erb strings, I get the error message saying that the method 'url' does not exist because @s3_direct_post is null.

Related posts

Recent Viewed