繁体   English   中英

ActiveAdmin中的Paperclip错误

[英]Paperclip errors in ActiveAdmin

我正在尝试使用Paperclip在ActiveAdmin Post编辑页面填写“图像”字段。 Rails 4.0.0,Paperclip 4.2.0。 在Post模型中,我添加了以下代码:

has_attached_file :image
validates_attachment :image, content_type: { content_type: [ "image/jpg", "image/jpeg", "image/png" ] }

提交表单后,我有以下错误:

Paperclip :: Admin :: PostsController#update中的错误

发布模型缺少attr_accessor需要'image_file_name'

看起来我忘了做某事。 在这一步我错过了什么? 好的,我手动添加了

attr_accessor :image_file_name

提交后我得到另一个错误

Admin :: PostsController#update中的NoMethodError

#Post的未定义方法`image_content_type':0x007fb148266e10

我不知道如何处理这个问题。

您的模型中不需要attr_accessor。 这应该足够了。

has_attached_file :image
validates_attachment :image, content_type: { content_type: [ "image/jpg", "image/jpeg", "image/png" ] }

还需要的是数据库中的特定列。 只需添加类似于此的迁移,它应该工作正常。

class AddImageToPosts < ActiveRecord::Migration
  def change
    add_attachment :posts, :image
  end
end

它会添加到您的模型中:

string   "image_file_name"
string   "image_content_type"
integer  "image_file_size"
datetime "image_updated_at"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM