Giacomo Carrozzo
Giacomo Carrozzo

Reputation: 27

model missing required attr_accessor for 'avatar_file_name'

On my app in production mode digitalocean I configured Paperclip but i have this problem:

I, [2016-04-08T15:17:29.169827 #23709]  INFO -- : Completed 500 Internal Server Error in 16ms (ActiveRecord: 0.6ms)
F, [2016-04-08T15:17:29.171143 #23709] FATAL -- :
Paperclip::Error (CommercialActivity model missing required attr_accessor for 'avatar_file_name'):
  app/controllers/commercial_activities_controller.rb:50:in `create'

commercial_activity.rb

class CommercialActivity < ActiveRecord::Base
   ...
  has_attached_file :avatar, styles: { medium: "300x300>", thumb: "100x100>" }, default_url: "/images/:style/missing.png"
  validates_attachment :avatar, content_type: { content_type: ["image/jpeg", "image/gif", "image/png"] }
   ...
end

Gemfile

gem "paperclip", git: "git://github.com/thoughtbot/paperclip.git"

commercial_activities_controller.rb

class CommercialActivitiesController < ApplicationController
   ...
  def create
      ...
     @commercial_activity = current_tradesman.commercial_activities.build(commercial_activity_params)
      ...
    respond_to do |format|
      if @commercial_activity.save
        format.html { redirect_to url_for(controller: :tradesmen, action: :index), notice: 'Commercial activity was successfully created.' }
        format.json { render :show, status: :created, location: @commercial_activity }
      else
        format.html { redirect_to url_for(controller: :tradesmen, action: :index), notice: 'Commercial activity was not created.' }
        format.json { render json: @commercial_activity.errors, status: :unprocessable_entity }
    end

  def commercial_activity_params
   params.require(:commercial_activity).permit(:avatar, ...)
  end
end

config\environments\production.rb

...
Paperclip.options[:command_path] = "/usr/bin/"
...

db\migrate\20160408154436_add_attachment_avatar_to_commercial_activities.rb

class AddAttachmentAvatarToCommercialActivities < ActiveRecord::Migration
  def self.up
    change_table :commercial_activities do |t|
      t.attachment :avatar
    end
  end

  def self.down
    remove_attachment :commercial_activities, :avatar
  end
end

How can I solve the problem? Thanks

Upvotes: 0

Views: 1981

Answers (3)

James Milani
James Milani

Reputation: 1943

So, are you sure you ran your migrations? When you run the migrations with a paperclip attachment, you should get four things: <attachment>_file_name, <attachment>_file_size, <attachment>_content_type, and <attachment>_updated_at. It seems like you're missing all of these...

Here's a link to the docs: https://github.com/thoughtbot/paperclip#usage

Upvotes: 0

James Milani
James Milani

Reputation: 1943

As a follow up to Anthony's answer, and the error reads this way, it looks like it wants an attr_accessor. However that seems strange to me because I've never needed one to get my paperclip images working. Have you tried giving the model an attr_accessible?

class CommercialActivity < ActiveRecord::Base
  ...
  attr_accessible :avatar
  ...
end

Upvotes: 0

Anthony E
Anthony E

Reputation: 11235

You need to either have a column or virtual attribute for avatar_file_name (eg attr_accessor :avatar_file_name) in your model. Paperclip will cache the form value in this field before saving it.

Upvotes: 1

Related Questions