Search code examples
ruby-on-rails-3.2asyncfileupload

How do I use Remotipart with paperclilp to uploaded image and show a thumbnail on the form


I am using paperclip and Rails 3.2

I have installed the Remotipart gem.

My Model that has an image attachement:

class Resource < ActiveRecord::Base
     has_attached_file :image, :styles => { :thumb => "50x50>", :small => "150x150>", :medium => "200x200>" },
                                :storage        => :s3,
                                :s3_credentials => "#{Rails.root}/config/s3.yml",
                                :path           => ':attachment/:id/:style.:extension'

      validates_attachment_presence :image
      validates_attachment_size :image, :less_than => 5.megabytes
      validates_attachment_content_type :image, :content_type => ['image/jpeg', 'image/png']
     end

Here is the form:

<%= form_for @resource, :html => { :class => 'form-horizontal' } do |f| %>
  <fieldset>
    <legend><%= controller.action_name.capitalize %> Resource</legend>

    <div class="control-group">
      <%= f.label :title, :class => 'control-label' %>
      <div class="controls">
        <%= f.text_field :title, :class => 'text_field' %>
      </div>
    </div>

    <div class="control-group">
      <%= f.label :url, :class => 'control-label' %>
      <div class="controls">
        <%= f.text_field :url, :class => 'text_field' %>
      </div>
    </div>

    <div class="control-group">
      <%= f.label :description, :class => 'control-label' %>
      <div class="controls">
        <%= f.text_field :description, :class => 'text_field' %>
      </div>
    </div>

    <div class="control-group">
      <%= f.label :author, :class => 'control-label' %>
      <div class="controls">
        <%= f.text_field :author, :class => 'text_field' %>
      </div>
    </div>

    <div class="control-group">
      <%= f.label :price, :class => 'control-label' %>
      <div class="controls">
        <%= f.number_field :price, :class => 'number_field' %>
      </div>
    </div>

    <div class="control-group">
      <%= f.label :category_id, :class => 'control-label' %>
      <div class="controls">
        <%= f.collection_select(:category_id, Category.all, :id, :name, {:prompt => 'Please select a Category'}) %>
      </div>
    </div>


  <div class="control-group">
      <%= f.label 'file types', :class => 'control-label' %>
      <div class="controls">      
        <% Filetype.all.each do |filetype| %>
        <%=check_box_tag "resource[filetype_ids][]", filetype.id, @resource.filetypes.include?(filetype) %>
        <%=filetype.abbreviation %>
      <% end %>
    </div>  
  </div>

 <div class="control-group">
      <%= f.label :image, :class => 'control-label' %>
      <div class="controls">   
        <%= f.file_field :image %>
      </div>
</div>
    <div class="form-actions">
      <%= f.submit nil, :class => 'btn btn-primary' %>
      <%= link_to 'Cancel', resources_path, :class => 'btn' %>
    </div>
  </fieldset>

I want to be able to upload the file when the file is selected and then show a thumbnail to the user. I understand that Remotipart is supposed to help make this easy but I have not idea how to make it work.

Anyone point me in the right direction?

Update: I want to be able to upload the file only using ajax so that a thumbnail can be shown of the uploaded image. So the form itself might not be submitted via ajax but the fileupload should.

Any way to do this?


Solution

  • Have you read the documentation for remotipart? It's for remote form uploads via ajax. You don't have a remote form.

    See https://github.com/JangoSteve/remotipart

    The good news is that you probably don't need to use remotipart at all!