使用回形针将file upload到Activeadmin Rails

我使用Activepipe理员作为我的Rails应用程序后端。 我想做一个file upload。 我怎样才能完成这个function?

我发现了一个使用Active Admin的Paperclip的方法。

我在模型“事件”中添加了这个代码:

has_attached_file :map, :styles => { :medium => "238x238>", :thumb => "100x100>" } 

我为我的pipe理模式做了这个:

 ActiveAdmin.register Event do form :html => { :enctype => "multipart/form-data" } do |f| f.inputs "Details" do f.input :continent f.input :event_type f.input :name f.input :title f.input :content f.input :date_start, :as => :date f.input :date_end, :as => :date f.input :place f.input :map, :as => :file f.input :image, :as => :file, :hint => f.template.image_tag(f.object.image.url(:medium)) f.input :userfull_info f.input :price f.input :phone, :as => :phone f.input :website, :as => :url end f.buttons end end 

要在索引页面上使用它,你必须使用:

 column "Image" do |event| link_to(image_tag(event.image.url(:thumb), :height => '100'), admin_event_path(event)) end default_actions end 

得到它适用于Rails 4.1和Paperclip 4.1:

模型

 class Hotel < ActiveRecord::Base has_attached_file :thumbnail, :styles => { :medium => "300x300#", :thumb => "200x200#" } validates_attachment :thumbnail, content_type: { content_type: ["image/jpg", "image/jpeg", "image/png"] } end 

pipe理模式

 ActiveAdmin.register Hotel do permit_params :name, :description, :price, :thumbnail form do |f| f.inputs "Project Details" do f.input :name f.input :thumbnail, :required => false, :as => :file # Will preview the image when the object is edited end f.actions end show do |ad| attributes_table do row :name row :thumbnail do image_tag(ad.thumbnail.url(:thumb)) end # Will display the image on show object page end end end 

我正在使用rails 3.0.1和下面的代码

 f.input :image, :hint => "current image: #{f.template.image_tag(f.object.image.url(:thumb))}" 

返回一个string。 search解决scheme后,我发现它。

 f.input :image, :hint => f.template.image_tag(f.object.image.url(:thumb)) 

直接发送对象,将图像返回给html

在最新版本的ActiveAdmin&Rails 4中显示文件字段,我们需要使用下面的代码

以前我们使用f.input:uploads,:as =>:file

 ActiveAdmin.register Project do permit_params :name, :uploads form multipart: true do |f| f.inputs "Project Details" do f.input :name f.input :uploads, required: false end f.actions end 

结束