繁体   English   中英

Ruby on Rails教程第6.3.4章

[英]Ruby on Rails Tutorial Chapter 6.3.4

创建用户帐户时,收到此警告。 这条消息是什么意思?

 SyntaxError: /Users/KOSUKE/workspace/score_share/app/models/user.rb:6: syntax error, unexpected ':', expecting keyword_end format: { with: VALID_EMAIL_REGEX } ^ /Users/KOSUKE/workspace/score_share/app/models/user.rb:7: syntax error, unexpected ':', expecting keyword_end uniqueness: { case_sensitive: false } ^ from /Users/KOSUKE/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:457:in `load' from /Users/KOSUKE/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:457:in `block in load_file' from /Users/KOSUKE/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:647:in `new_constants_in' from /Users/KOSUKE/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:456:in `load_file' from /Users/KOSUKE/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:354:in `require_or_load' from /Users/KOSUKE/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:494:in `load_missing_constant' from /Users/KOSUKE/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:184:in `const_missing' from (irb):1 from /Users/KOSUKE/.rvm/gems/ruby-2.2.1/gems/railties-4.2.2/lib/rails/commands/console.rb:110:in `start' from /Users/KOSUKE/.rvm/gems/ruby-2.2.1/gems/railties-4.2.2/lib/rails/commands/console.rb:9:in `start' from /Users/KOSUKE/.rvm/gems/ruby-2.2.1/gems/railties-4.2.2/lib/rails/commands/commands_tasks.rb:68:in `console' from /Users/KOSUKE/.rvm/gems/ruby-2.2.1/gems/railties-4.2.2/lib/rails/commands/commands_tasks.rb:39:in `run_command!' from /Users/KOSUKE/.rvm/gems/ruby-2.2.1/gems/railties-4.2.2/lib/rails/commands.rb:17:in `<top (required)>' from /Users/KOSUKE/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:274:in `require' from /Users/KOSUKE/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:274:in `block in require' from /Users/KOSUKE/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:240:in `load_dependency' from /Users/KOSUKE/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:274:in `require' from /Users/KOSUKE/workspace/score_share/bin/rails:9:in `<top (required)>' from /Users/KOSUKE/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:268:in `load' from /Users/KOSUKE/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:268:in `block in load' from /Users/KOSUKE/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:240:in `load_dependency' from /Users/KOSUKE/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:268:in `load' from /Users/KOSUKE/.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require' from /Users/KOSUKE/.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require' from -e:1:in `<main>'2.2.1 :002 > 

确保您的user.rb文件看起来完全像这样:

class User < ActiveRecord::Base
  before_save { self.email = email.downcase }
  validates :name, presence: true, length: { maximum: 50 }
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  validates :email, presence: true, length: { maximum: 255 },
                    format: { with: VALID_EMAIL_REGEX },
                    uniqueness: { case_sensitive: false }
  has_secure_password
  validates :password, presence: true, length: { minimum: 6 }
end

该错误消息告诉您, user.rb包含语法错误:

SyntaxError: /app/models/user.rb:6: syntax error, unexpected ':', expecting keyword_end
format: { with: VALID_EMAIL_REGEX }
      ^
/app/models/user.rb:7: syntax error, unexpected ':', expecting keyword_end
uniqueness: { case_sensitive: false }

你可能只是错过了一个,或关闭}

重新访问user.rb (尤其是5-7行),并仔细检查它是否与书中示例完全相同。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM