carrierwave - rails 3.1- undefined method: image_will_change

buren picture buren · Sep 20, 2011 · Viewed 17.5k times · Source

I get an error that look like this:

undefined method `post_image_will_change!' for #<Post:0xf4e9184>
app/controllers/posts_controller.rb:43:in `new'
app/controllers/posts_controller.rb:43:in `create'

I've included this in my "post" model:

 attr_accessible :title, :name, :content, :post_image
 mount_uploader :post_image, PostImageUploader

and in _form.html.erb I've added:

 :html =>  { :multipart => true }

I looked CarrierWave Error but that doesn't help me.

Any clues of what generates that error? I've migrated the database and so forth (followed the railscasts guide on carrierwave exactly..)

Answer

kreek picture kreek · May 16, 2012

The OP comments that he fixed it, however there's no answer set so I thought I'd add one for people coming across this in the future, which included myself until I figured it out :)

undefined method `x_will_change!' for # happens if you forget to add a column in your model's db table. If you have a model User and a AvatarUploader, with the uploader mounted as in the Carrierwave docs:

class User < ActiveRecord::Base
  mount_uploader :avatar, AvatarUploader
end

Then the error will read

undefined method `avatar_will_change!' for #<User:0x00...>

To fix it (based on this example) add a column in a migration run the following in the console:

rails g migration AddAvatarToUsers avatar:string

This will generate the following migration:

class AddAvatarToUsers < ActiveRecord::Migration
  def change
    add_column :users, :avatar, :string
  end
end

Then migrate to apply the change (again in the console):

rake db:migrate