在active_admin界面中上传照片

时间:2013-07-11 07:12:29

标签: ruby-on-rails file-upload activeadmin

我不想在活动管理界面中添加图片上传字段。This is the view where I want to be able get photo upload

我尝试了一些早期的建议From here

ActiveAdmin.register Product do
  form :html => { :multipart=>true } do |f|
  f.inputs :new_product  do
  f.input :name
  f.input :price
  f.input :category
  f.input :description

  f.has_many :prod_images  do |p|
    p.input :photo, :as => :file, :label => "Image",:hint =>   p.template.image_tag(p.object.photo.url(:thumb)) 
    p.input :_destroy, :as=>:boolean, :required => false, :label => 'Remove image'
  end
end

f.buttons  

使用这个例子我得到了这样的错误

undefined method `klass' for nil:NilClass

它说错误来自app/views/active_admin/resource/new.html.arb where line #1 raised 但是如何访问该文件,因为在资源管理器中它没有出现? 感谢

2 个答案:

答案 0 :(得分:0)

尝试构建一个prod图像,就像这样。

f.has_many :prod_images, f.object.prod_images.build do |p|

答案 1 :(得分:0)

我设法使用此代码获取文件上传字段

ActiveAdmin.register Product do
 form :html => { :enctype => "multipart/form-data" } do |f|
f.input :photo, :as => :file, :hint => f.template.image_tag(f.object.photo.url(:thumb))
end

但现在我无法添加提交按钮:D所以我还在努力:)

修改

ActiveAdmin.register Product do
  form :html => { :enctype => "multipart/form-data" } do |f|
    f.input :photo, :as => :file
    f.buttons
  end
end

这只显示创建和取消等按钮,但没有显示文件字段,我检查了形式化示例,但没有成功。

<强> EDIT2

class Product < ActiveRecord::Base
  attr_accessible :category_id, :description, :manufacturer_id, :name, :photo 
  extend FriendlyId
   has_attached_file :photo,
     :styles => {
     :thumb=> "100x100#",
     :large => "290x170",
     :medium=> "120x120"}

  friendly_id :name, use: [:slugged, :history]
  belongs_to :manufacturer
  belongs_to :category
end