carrierwave - rails 3.1- undefined method: image_will_change
Asked Answered
C

5

36

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..)

Compassionate answered 20/9, 2011 at 20:50 Comment(4)
can you please share your posts_controller.rb file?Crabby
did u add the new migration files for image? :)Dayfly
manged to fix it.. had made an extreme rookie misstake. Ty anywayCompassionate
What mistake was that. I'm having the same issueHooray
H
133

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
Hereto answered 15/5, 2012 at 23:47 Comment(2)
nice 1+ forgot to add image column here aswellRinghals
Switched branches and reset db in other branch, forgot to migrate in this one. Thanks! :)Rottweiler
R
6

I suppose that author just forgot to run:

rake db:migrate

ALso, if you met such error inside of your tests then you should run:

rake db:test:prepare
Regen answered 25/9, 2012 at 14:4 Comment(0)
D
6

Also, for anyone getting this error on heroku, you need to run

heroku run rake db:migrate

and

heroku restart

in the terminal after adding/removing fields/tables from your database.

Digamy answered 28/6, 2013 at 20:37 Comment(1)
saved the day. Thank you!Kaif
N
0

Kreek, this is obviously a minor oversight, as most people would have realized by now, you probably meant to run this command, as one should, outside the console, otherwise, one would get the following:

'NameError: undefined local variable or method `migrate' for main:Object'.

Nairn answered 10/7, 2013 at 12:25 Comment(0)
K
0

I had similar problem but mine was because I was copying and pasting codes and forgot to delete

mount_uploader :picture, PictureUploader

from my model which did not use pictures. Hope this help others in future who could not figure out what happened

Krems answered 7/7, 2019 at 11:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.