Seb*_*ien 41 ruby ruby-on-rails activeadmin
我使用Active admin作为我的rails应用程序后端.我想要上传文件.我该如何完成此功能?
Seb*_*ien 75
我找到了一种方法来使用Paperclip和Active Admin.
我在我的模型"Event"中添加了这段代码:
has_attached_file :map, :styles => { :medium => "238x238>",
:thumb => "100x100>"
}
Run Code Online (Sandbox Code Playgroud)
我为我的管理模型做了这个:
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
Run Code Online (Sandbox Code Playgroud)
要在索引页面上使用它,您必须使用:
column "Image" do |event|
link_to(image_tag(event.image.url(:thumb), :height => '100'), admin_event_path(event))
end
default_actions
end
Run Code Online (Sandbox Code Playgroud)
kok*_*uke 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
Run Code Online (Sandbox Code Playgroud)
管理模型
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
Run Code Online (Sandbox Code Playgroud)
我正在使用rails 3.0.1和以下代码
f.input :image, :hint => "current image: #{f.template.image_tag(f.object.image.url(:thumb))}"
Run Code Online (Sandbox Code Playgroud)
返回一个字符串.在搜索解决方案后,我找到了它.
f.input :image, :hint => f.template.image_tag(f.object.image.url(:thumb))
Run Code Online (Sandbox Code Playgroud)
直接发送对象,将图像返回给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
end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
28907 次 |
| 最近记录: |