Sign in through GitHub

Please read for an updated status on RailsCasts:

Learn more or hide this

tquill's Profile

GitHub User: tquill

Comments by

Avatar

After a lot of time on google, I think I finally got this addressed:

add content_type to upload_helpber.rb (fields method and policy_data method):

ruby
def fields
  {
    :key => key,
    :acl => @options[:acl],
    :policy => policy,
    :content_type => nil,
    :signature => signature,
    "AWSAccessKeyId" => @options[:aws_access_key_id],
  }
end

def policy_data
  {
    expiration: @options[:expiration],
    conditions: [
      ["starts-with", "$utf8", ""],
      ["starts-with", "$Content-Type", ""],
      ["starts-with", "$key", ""],
      ["content-length-range", 0, @options[:max_file_size]],
      {bucket: @options[:bucket]},
      {acl: @options[:acl]}
    ]
  }
end

add the two lines above "data.submit()" in the add method in the coffeescript file

coffee
add: (e, data) ->
  ...
   $('#fileupload').append(data.context)
   data.form.find('#content_type').attr('name','Content-Type')
   data.form.find('#content_type').val(file.type)
   data.submit()
Avatar

Did you ever figure this out? I'm looking to do the same thing.

Avatar

I figured it out.

First, change the target for your form (leave the "post" though).

<div id="upload_form">

  <%= s3_uploader_form post: user_url(@user), as: "user[avatar]" do %>
    <%= file_field_tag :file, multiple: false %>
  <% end %>

  <script id="template-upload" type="text/x-tmpl">
    <div class="upload">
      {%=o.name%}
      <div class="progress"><div class="bar" style="width: 0%"></div></div>
    </div>
  </script>

</div>

In the javascript file (I converted it from coffee script) change this line in the done callback:

$.post(to, content);

to

jQuery.ajax({
  type: "PUT",
  url: to,
  data: content
});
Avatar

Did you ever figure this out? I'm looking to do the same thing.