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

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

Related

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.

carrierwave - rails 3.1- undefined method: image_will_change

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

Ruby NameError with BSON

I am new to ruby so I am sorry if this question has an obvious answer but I have not had much luck with this.
But I keep running into
uninitialized constant BSON::ObjectID (NameError)
I have the require
require 'mongo'
Then here is the piece of code that's throwing the error, collection of course points to a db.
#Insert and return the row.
def insert(row)
id = collection.insert row
collection.find_one(:_id => BSON::ObjectID.from_string(id.to_s))
end
I am totally at a loss for whats wrong here. seeing how ruby doesn't like me requiring bson before or after mongo.
Tried in 1.9.2 and 1.8.7
Update: It's Id (upper, then lower case), not ID.
Just to be sure about it, put the require 'mongo' in the same file as the code block above, and then change BSON::ObjectID to ::BSON::ObjectId.
Try a require 'bson' in the top of your file.
Was not able to 100% solve the issue, but id was already a BSON::ObjectID so I just changed it to
collection.find_one(:_id => id)
Works as I would like it to now!

Creating a plist in Rails 3

I'm new to Rails and I'm trying to learn it using Rails 3 (RC).
I have managed to use http://plist.rubyforge.org/ for supporting the output of plists. I would like to check with you guys to see if my approach is the right way to do it. Here goes:
In the gemfile I added gem 'plist'
In config/initializers/mime_types.rb I added Mime::Type.register "application/plist", :plist
In the controller, I added format.plist { render :plist => #product } in show
In the model, I added
def to_plist
attributes.to_plist
end
And finally, in the view file show.plist.erb, I have <%= raw #product.to_plist %>
Accessing for instance /products/2.plist works fine, but being new to Rails, I'm wondering if there are anything I should have done differently.
Looks right to me.
The only suggestion I have is to perhaps mix in the to_plist method into ActiveRecord::Base so that you don't have to define it over and over in each model. Perhaps this method will even support render_with syntax?
I'm about to do something similar myself.

Requiring gem in Rails 3 Controller failing with "Constant Missing"

I've seen this asked a few times in other threads, but none of the answers seem to apply.
Environment:
Rails 3
amazon/ecs gem from jugend. The lone file is here:
http://github.com/jugend/amazon-ecs/blob/master/lib/amazon/ecs.rb
my gemfile has:
gem 'amazon-ecs', :git => 'git://github.com/jugend/amazon-ecs.git'
Everything works in irb. I can run:
bundle console
require 'amazon/ecs' and then go to town
when I try to use it from the controller though, like so:
require 'amazon/ecs'
require 'amazon/ecs'
class SearchController < ApplicationController
def index
end
def results
Amazon::Ecs.configure do |options|
options[:aWS_access_key_id] = '[key]'
options[:aWS_secret_key] = '[secret]'
end
res = Amazon::Ecs.item_search(params[:search], {:response_group => 'Medium', :search_index => 'All'})
end
end
I get: uninitialized constant SearchController::Amazon at line 8, where I first try to use Amazon.
the ecs.rb has a module Amazon containing a class Ecs. I'm not sure why this is working in erb, and not in rails.
I'm still kinda new to Rails, so please answer using small words. :-/
Was given the answer. I moved my initialization code to an initializer in config/initializers file, removed the require entirely, and things worked. I don't know why though, so if someone could answer that, that'd be great.
All of the gems require their files by default, so usually you don't need to explicitly require any files.
Speaking about your problem, it could somehow be, that your controller is run before Amazon module is processed.

Resources