ActiveAdmin 中的回形针错误

fem*_*che 1 ruby-on-rails paperclip activeadmin

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

has_attached_file :image
validates_attachment :image, content_type: { content_type: [ "image/jpg", "image/jpeg", "image/png" ] }
Run Code Online (Sandbox Code Playgroud)

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

回形针 :: Admin 中的错误::PostsController#update

发布模型缺少 'image_file_name' 所需的 attr_accessor

好像忘记做点什么了。在这一步我错过了什么?好的,我已经手动添加了

attr_accessor :image_file_name
Run Code Online (Sandbox Code Playgroud)

提交后我收到另一个错误

Admin::PostsController#update 中的 NoMethodError

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

我不知道该怎么办这个。

小智 5

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

has_attached_file :image
validates_attachment :image, content_type: { content_type: [ "image/jpg", "image/jpeg", "image/png" ] }
Run Code Online (Sandbox Code Playgroud)

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

class AddImageToPosts < ActiveRecord::Migration
  def change
    add_attachment :posts, :image
  end
end
Run Code Online (Sandbox Code Playgroud)

它将添加到您的模型中:

string   "image_file_name"
string   "image_content_type"
integer  "image_file_size"
datetime "image_updated_at"
Run Code Online (Sandbox Code Playgroud)