2012-03-07 42 views
0

我使用回形針和Rails 3.2如何使用Remotipart與paperclilp於上傳的圖片,並顯示形式

我已經安裝了Remotipart寶石上的縮略圖。具有圖像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 

下面的形式:

<%= 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> 

我要當選擇該文件能夠上傳文件,然後顯示縮略圖給用戶。我知道Remotipart應該可以幫助簡化這個過程,但我不知道如何使其工作。

任何人點我在正確的方向?

更新: 我想只能使用ajax上傳文件,以便可以顯示上傳圖像的縮略圖。所以表單本身可能不會通過ajax提交,但文件上傳應該。

任何方式來做到這一點?

回答

0

您是否閱讀過remotipart的文檔?它用於通過ajax進行遠程表單上傳。您沒有遠程表單。

https://github.com/JangoSteve/remotipart

的好消息是,你可能並不需要remotipart在所有使用!

+0

你是對的我不會用它感謝你的擡頭。 – chell 2012-03-19 05:32:42