emilioforrer / haml_coffee_assets

Haml Coffee templates in the Rails asset pipeline or as Sprockets engine.
https://rubygems.org/gems/haml_coffee_assets
MIT License
438 stars 145 forks source link

Using Rails form helper methods inside jst.hamlc files #122

Closed JigneshGohel-BoTreeConsulting closed 10 years ago

JigneshGohel-BoTreeConsulting commented 10 years ago

@netzpirat

In my sample application (based on Rails 3.2+, Backbone and CoffeeScript) I need to use the gem s3_direct_upload which supplies a form helper named s3_uploader_form This form helper I need to use inside one of template(having .jst.hamlc) corresponding to a Backbone view.

So my question is, is it possible to use Rails form helper tags like form_for inside jst.hamlc files? If not, then can you please suggest on how I can I manage to achieve the above requirement?

Thanks, Jignesh

netzpirat commented 10 years ago

You can't use Rails helpers in the browser, because they are designed to be rendered at the backend and not on the client. I've implemented successful S3 uploads in single page apps in two ways:

  1. Read the file data with the HTML5 file apI (and a SWF implementation as fallback for older browsers) to get the data, send it through a REST API to the backend and upload it from there to S3. This is useful if you want to display a preview of the file and allow manipulation of the data within the browser without upload it first to the backend, and also want to process the data in the backend before uploading, like generating thumbnails...
  2. Create a bucket policy and signature for the upload in the backend and pass it to the client, which now can directly POST to S3, see http://docs.aws.amazon.com/AmazonS3/latest/dev/UsingHTTPPOST.html