femalemoustache
femalemoustache

Reputation: 591

Paperclip errors in ActiveAdmin

I'm trying to use Paperclip to fill in "image" field at ActiveAdmin Post edit page. Rails 4.0.0, Paperclip 4.2.0. In Post model i've added following code:

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

After submitting form i have following error:

Paperclip::Error in Admin::PostsController#update

Post model missing required attr_accessor for 'image_file_name'

Looks like I've forgotten to do something. What've i missed to do at this step? Ok, I've manually added

attr_accessor :image_file_name

After submitting i get another error

NoMethodError in Admin::PostsController#update

undefined method `image_content_type' for #Post:0x007fb148266e10

I don't know what to do with this one.

Upvotes: 1

Views: 1539

Answers (1)

Mafi
Mafi

Reputation: 196

You don't need attr_accessor in your model. This should be enough.

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

What is still needed are specific columns in database. Just add migration similar to this and it should be working fine.

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

It will add to your model:

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

Upvotes: 4

Related Questions