0

I am building a blog that allows me to post pictures, and when a new post w/ a picture comes up, I get an error saying "require attr_accessor for 'image_file_name'". So I put my "attr_accessor :image" in there and I get an "undefined method error 'attr_accessor'". https://github.com/BBaughn1/savagelysaving <-- the github if you need it

def create
    @post = Post.new(post_params)
    if @post.save
      redirect_to @post
    else
      render 'new'
    end
  end

This is before the error,

def create
    attr_accessor :image
    @post = Post.new(post_params)
    if @post.save
      redirect_to @post
    else
      render 'new'
    end
  end

and this is after the error. And, I got this error earlier but fixed it with this:

def update
    @post = Post.find(params[:id])
    attr_accessor :image
    if @post.update(params[:post].permit(:title, :body, :image))
      redirect_to @post
    else
      render 'edit'
    end
  end

putting "attr_accessor :image" before the "@post = Post.new(post_params)" will give me the same error saying that I require "attr_accessor"

Brendon Baughn
  • 150
  • 1
  • 13
  • wasn't what I was looking for but thanks – Brendon Baughn Sep 16 '15 at 21:32
  • says it must by "CONSTANT" – Brendon Baughn Sep 16 '15 at 21:38
  • possible duplicate of [Paperclip Error: model missing required attr\_accessor for 'avatar\_file\_name'](http://stackoverflow.com/questions/22746393/paperclip-error-model-missing-required-attr-accessor-for-avatar-file-name) – Brad Werth Sep 18 '15 at 02:40
  • That duplicate made it worse. I took what I read (Destroy the existing migration, if any: rails destroy paperclip listing avatar, then I needed to generate a new migration: rails generate paperclip listing avatar, and finaly rake db:migrate) and I ended up getting an error saying "rake aborted!".... – Brendon Baughn Sep 18 '15 at 02:54
  • and for every "avatar," I did "image" – Brendon Baughn Sep 18 '15 at 02:56

1 Answers1

1

It looks like this is simply a naming mismatch. Your schema shows you have the attributes avatar_file_name, avatar_content_type, avatar_file_size, and avatar_updated_at. But in your model, you have has_attached_file :image. These two items should match up. it sounds like you probably want has_attached_file :avatar, instead. You will need to replace other relevant occurrences of "image" with "avatar" in your views and controllers, as well. You also should remove the attr_accessor :image from your model. Your error indicates that the required fields do not exist in the database, for the structure you have defined.

Brad Werth
  • 17,411
  • 10
  • 63
  • 88
  • "Avatar" or "image", it doesn't matter. You could call it "turkey_leg" if you really wanted, but you need to be consistent. – Brad Werth Sep 18 '15 at 03:45