carrierwave - rails 3.1- undefined method: image_will_change - ruby-on-rails-3.1

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

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

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

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.

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

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

Related

Rails gem - Public Activity for earlier posts

The gem does not recognize earlier post which were created before the gem was added.
It only started working, when fresh posts were created. Why was that?
And, how to have those earlier posts get covered by public_activity
Thanks.
Gem setup according to author site.
You've to create the old activities manually using create_activity method. I created a rake task for this.
task public_activity_migration: :environment do
User.find_each do |user|
[:comments, :friends].each do |relation|
user.send(relation).find_each do |obj|
obj.create_activity :create, owner: user, created_at: obj.created_at
print "."
end
end
end
end
The code above will create activities for the comment and friend model. If you're not using strong params you also need to allow the created_at attribute to be set on the PublicActivity::Activity model. This can be done by adding the following code before running your task.
class PublicActivity::Activity
attr_accessible :created_at
end

When using filepicker-rails, where should I define my avatar_url method within my rails app?

I've installed the filepicker-rails gem and I am able to upload files to the filepicker.io server. When I try to display the image for different users profile, I get the error:
undefined method 'avatar_url' for nil:NilClass
Under my users directory, in the show.html.erb file I have:
<%= filepicker_image_tag #user.avatar_url, w: 160, h: 160, fit: 'clip' %>
I have the following under my User.rb file:
def avatar_url(user)
user.avatar_url
end
Any ideas why this doesn't work?
Basically you'll need a column in your DB called avatar_url. Write a migration that will give you a avatar_url column as just a regular string column. Since you're using Rails, ActiveRecord will provide the avatar_url method for you.
Edit: In your controller you're likely not looking up your user correctly which is resulting in a method call on a NilClass in your view.

method authenticate misssing from User model using secure_password

Using Rails 3.1.3. I'm using the method of password authentication presented in railscast 270 to place the call to "has_secure_password":
class User < ActiveRecord::Base
has_secure_password
end
It works except for one extremely puzzling issue. The documentation states to use
user.authenticate("notright")
But I can't see where the authenticate method is declared. I looked in secure_password, and a seemingly equivalent method is:
# Returns self if the password is correct, otherwise false.
def get_logged_in_user(unencrypted_password)
if BCrypt::Password.new(password_digest) == unencrypted_password
self
else
false
end
end
Note, I'm trying to convert the example from the railstutorial.org (really wonderful), so there was quite a bit I had to change, and there could be something in my application that is conflicting. For example, I had previously had this line:
attr_accessor :password
And that prevented the declaration of password= from ever being called in secure_password, resulting in the db column for password_digest being nil. Removing this line fixed this problem.
Update: I created a brand new rails app with 3.1.3, and confirmed that the simplest case of running these two commands results in the error:
u = User.create(:username => "a", :password => "foobar", :password_confirmation => "foobar")
(0.1ms) BEGIN
SQL (0.2ms) INSERT INTO users (created_at, password_digest, updated_at, username) VALUES ('2011-12-21 05:18:17', '$2a$10$BbwHbq1bGwvQRgE0xK28VeP8K/lwY.VfLaLsMSs6ogNa1DucephnK', '2011-12-21 05:18:17', 'a')
(42.6ms) COMMIT
u.authenticate("foobar")
u.authenticate("foobar")
NoMethodError: undefined method authenticate' for #<User:0xa26e4cc>
from /home/justin/.rvm/gems/ruby-1.9.3-p0#testapp/gems/activemodel-3.1.3/lib/active_model/attribute_methods.rb:385:inmethod_missing'
from /home/justin/.rvm/gems/ruby-1.9.3-p0#testapp/gems/activerecord-3.1.3/lib/active_record/attribute_methods.rb:60:in method_missing'
from (irb):4
from /home/justin/.rvm/gems/ruby-1.9.3-p0#testapp/gems/railties-3.1.3/lib/rails/commands/console.rb:45:instart'
from /home/justin/.rvm/gems/ruby-1.9.3-p0#testapp/gems/railties-3.1.3/lib/rails/commands/console.rb:8:in start'
from /home/justin/.rvm/gems/ruby-1.9.3-p0#testapp/gems/railties-3.1.3/lib/rails/commands.rb:40:in'
from /home/justin/j/RubymineProjects/auth/script/rails:6:in require'
from /home/justin/j/RubymineProjects/auth/script/rails:6:in'
from -e:1:in load'
from -e:1:in'
UPDATE:
This is definitely some sort of issue with ruby 1.9.3. I tried the same gems out on 1.9.2 and I have no issues.
Adding an authenticate method as follows (source) solved it for me.
I'm using ruby 1.9.3 as well. I've not tried 1.9.2 as you have.
Upgrading to Ruby 1.9.3 solve the issue.

rake aborted! uninitialized constant Object::Country, why can't see model?

I have rails 3.1, I am trying to populate data with seeds.rb
I have a model Country which is migrated into a countries table
But it seems that rails can't see Country model from seeds.rb
I get this error:
rake aborted!
uninitialized constant Object::Country
Tasks: TOP => db:seed
my seeds.rb file looks like this:
# encoding: UTF-8
Country.delete_all
my Country model:
class Country < ActiveRecord::Base
has_many :students
has_many :instructors
end
any idea please ?
EDIT
I am in development environment, with config.threadsafe!

undefined local variable or method 'within' for User:class

I am working with a book to teach myself Ruby-on-Rails. Ruby version is 1.2.3 and rubygems V 1.3.5.
I start the console by ruby script/console and enter:
user = User.new(:screen_name => "example",
?> :email => "exampleATexample.com",
?> :password => "example")
but instead of adding the data to the DB, I get the following:
NameError: undefined local variable or method 'within' for User:Class from D:/ruby/lib/gems/1.8/gems/activerecord-1.15.3/lib/active_record/base.rb:1235:in 'method_missing' from ./script/../config/../config/../app/modules/user.rb:13
I don't really understand what's going on. Any kind of help is much appreciated, thank you!
First, I believe you main Rails v 1.2.3, this is a very old version of Rails, don't use it.
Second thing, the command you entered has wrong Ruby syntax... try this instead:
user =User.new(:screen_name => "example", :email => "exampleATexample.com", :password => "example")
Third thing: please paste more code....
Also, just as a check.
Whenever something doesn't work in Rails that has to do with the database, make sure you run
rake db:migrate
Sometimes this can slip your mind and cause weird errors.
Also, just as a side note... I would make sure that you add some validations to your user model that requires a user to confirm their password and or email. It's generally a good practice.
validates_confirmation_of :password, :email
All the best with learning Rails. Make sure you take #dvyjones's advice and upgrade to the latest version of Rails... a lot has changed since v. 1.2.3

Resources