I am following the https://devcenter.heroku.com/articles/direct-to-s3-image-uploads-in-rails#jquery-file-upload-callbacks Tutorial to use s3_direct_upload on local development environment. There's some javascripts which set the fileupload function for each file fields. It all works great if I simply include the codes right below the form, but it just doesn't work if I put the code in a js.erb file under app/assets/javascripts/. The error is:
ActionView::Template::Error (undefined method `url' for nil:NilClass
(in /s3_direct_upload_gem_test/app/assets/javascripts/s3_direct_upload.js.erb)):
18:
19: <%= @s3_direct_post.url %>
20:
21: <%= javascript_include_tag :s3_direct_upload %>
app/assets/javascripts/s3_direct_upload.js.erb:14:in `block in singleton class'
As you may see, line 19 from the above code is used to print out the @s3_direct_post.url. It was correctly printed when I include all the javascript in the file. If I trace the line 14 in the s3_direct_upload.js.erb, it is the url line:
fileInput.fileupload({
fileInput: fileInput,
url: '<%= @s3_direct_post.url %>',
type: 'POST',
autoUpload: true,
formData: <%= @s3_direct_post.fields.to_json.html_safe %>,
It seems like that for some reason, the javascript file under the assets folder (in the asset pipeline) is compiled separately and so @s3_direct_post, which is set in the Controller is not set here.
Of course I can always put those in <script> at the viewer file but it is not quite elegant. Is there any way to separate page-specific js coding and solve the problem above?
<% p self.class %>before errored ?