开发者

NameError: uninitialized constant User::SCREEN_NAME_RANGE

开发者 https://www.devze.com 2023-03-31 02:14 出处:网络
So according to the tutorial on Railsspace, I am suppose to use the console to understand Active Records. So I do what that things says I should do and this is the result I get.

So according to the tutorial on Railsspace, I am suppose to use the console to understand Active Records. So I do what that things says I should do and this is the result I get.

Does anyone know where the problem is?

   $ rails console

Output

Loading development environment (Rails 3.0.10)
ruby-1.9.2-p290 :001 > user = User.new(:screen_name => "me",
ruby-1.9.2-p290 :002 > 开发者_如何学编程    :email => "",
ruby-1.9.2-p290 :003 >     :password => "a",)
NameError: uninitialized constant User::SCREEN_NAME_RANGE
    from /Users/dennisbuizert/Sites/gpoff/app/models/user.rb:3:in `<class:User>'
    from /Users/dennisbuizert/Sites/gpoff/app/models/user.rb:1:in `<top (required)>'
    from (irb):1
    from /Users/dennisbuizert/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.0.10/lib/rails/commands/console.rb:44:in `start'
    from /Users/dennisbuizert/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.0.10/lib/rails/commands/console.rb:8:in `start'
    from /Users/dennisbuizert/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.0.10/lib/rails/commands.rb:23:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'

User model

    class User < ActiveRecord::Base
  validates_uniqueness_of :screen_name, :email
  validates_length_of     :screen_name, :within => SCREEN_NAME_RANGE
  validates_length_of     :password,    :within => PASSWORD_RANGE
  validates_length_of     :email,       :maximum => EMAIL_MAX_LENGTH
  validates_presence_of   :email

  # Max & Min length for all fields
  SCREEN_NAME_MIN_LENGTH  = 3
  SCREEN_NAME_MAX_LENGTH  = 20
  PASSWORD_MIN_LENGTH     = 8
  PASSWORD_MAX_LENGTH     = 26
  EMAIL_MAX_LENGTH        = 50
  SCREEN_NAME_RANGE = SCREEN_NAME_MIN_LENGHT..SCREEN_NAME_MAX_LENGTH
  PASSWORD_RANGE = PASSWORD_MIN_LENGHT..PASSWORD_MAX_LENGTH

end

I am using SQLite3 rather than MySQL, because I cannot figure out how to replace SQLite3 with mysql and how to get that working.


I see where the error is coming from. You must declare your constants before your validations. When the class is loaded, the constant you reference in your validation have not been declared yet, hence the error.

Your code should look like this:

class User < ActiveRecord::Base

  # Max & Min length for all fields
  SCREEN_NAME_MIN_LENGTH  = 3
  SCREEN_NAME_MAX_LENGTH  = 20
  PASSWORD_MIN_LENGTH     = 8
  PASSWORD_MAX_LENGTH     = 26
  EMAIL_MAX_LENGTH        = 50
  SCREEN_NAME_RANGE = SCREEN_NAME_MIN_LENGHT..SCREEN_NAME_MAX_LENGTH
  PASSWORD_RANGE = PASSWORD_MIN_LENGHT..PASSWORD_MAX_LENGTH

  validates_uniqueness_of :screen_name, :email
  validates_length_of     :screen_name, :within => SCREEN_NAME_RANGE
  validates_length_of     :password,    :within => PASSWORD_RANGE
  validates_length_of     :email,       :maximum => EMAIL_MAX_LENGTH
  validates_presence_of   :email

end


To get sqlite working, make sure you have the line gem sqlite3 in your Gemfile, run bundle install, and make your sections in config/database.yml look something like this:

development:
  adapter: sqlite3
  database: db/development.sqlite3
  pool: 5
  timeout: 5000

As for the SCREEN_NAME_RANGE error, I can't tell where that's coming from without seeing your code (assuming that the above config changes don't fix it). If you're absolutely sure your files are exactly like they're shown in the tutorial, I'd contact the author since it's a non-free doc set.

You might want to check out http://guides.rubyonrails.org/ . They have big warnings that they're incomplete, but for the most part they're pretty well-written (and by publicly active contributing members of the community), and free.


this problem occurs when editing model files while autotest is running

So just stop autotest then start it again (if you want)

0

精彩评论

暂无评论...
验证码 换一张
取 消