使用paperclip通过Activeadmin Rails上传文件

41

我使用Active Admin作为我的Rails应用程序后端。我想要实现文件上传功能。请问我该如何做?


我从未特别使用过Active Admin,但是Paperclip gem可以帮助您轻松上传文件并将其附加到任何模型。https://github.com/thoughtbot/paperclip - airlok
4个回答

75

我找到了一种在Active Admin中使用Paperclip的方法。

我在我的模型"Event"中添加了这段代码:

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

我为我的管理员模型执行了以下操作:

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

4
你可以使用 f.input :image, :hint => "当前图片: #{f.template.image_tag(f.object.image.url(:thumb))}"。 - Nazar
这个使用Paperclip默认会上传到S3吗? - Pinser
2
我必须使用"f.actions"而不是"f.buttons"才能使其工作。 - Joseph
这看起来很不错,但如果您只是选择了图像而没有上传图像,它就无法预览图像。有什么办法可以解决这个问题吗? - Tiago

13

已在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

管理模型

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

我遇到了“Paperclip NoHandlerError”错误, 我不得不使用form :html => {:multipart => true} do |f| - givanse
@givanse 你需要在哪里写 form :html ... - Aleks

6
我使用的是 Rails 3.0.1 版本,以下是代码:

```

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

返回一个字符串。在寻找解决方案后,我找到了它。

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

直接发送对象,将返回一个图像到HTML。

2
你可以使用第一行代码,只需在字符串(双引号后)调用 html_safe - Brett Bender

5
在ActiveAdmin和Rails 6的最新版本中,为了显示文件字段,我们需要使用以下代码:

```<%= f.input :file_field_name, 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, as: :file, required: false
    end
    f.actions
  end

end

在一些旧版本的AA中,以下代码也可以正常工作。
f.input :uploads, required: false

不起作用,你仍然需要在 Rails 6 中添加 as: :file。 - Aditya Joardar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接