我使用Active Admin作为我的Rails应用程序后端。我想要实现文件上传功能。请问我该如何做?
我使用Active Admin作为我的Rails应用程序后端。我想要实现文件上传功能。请问我该如何做?
我找到了一种在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
已在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
form :html => {:multipart => true} do |f|
。 - givanseform :html ...
? - Aleks```
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_safe
。 - Brett Bender```<%= 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