Rails 3:唯一性选项吐出语法错误

时间:2010-11-28 07:09:38

标签: ruby-on-rails syntax-error

我正在试图弄清楚为什么这个唯一性选项会失败并且已经用尽所有选项。任何人都知道为什么这可能会在rspec中出错?

非常感谢,

$ rspec spec
/Library/Ruby/Gems/1.8/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:454:in `load': /Users/habitat/Documents/RoR/tut/app/models/user.rb:10: syntax error, unexpected tASSOC, expecting kEND (SyntaxError)
                    :uniqueness => { :case_sensitive => false }
                                  ^
    from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:454:in `load_file'
    from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:596:in `new_constants_in'
    from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:453:in `load_file'
    from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:340:in `require_or_load'
    from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:300:in `depend_on'
    from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:216:in `require_dependency'
    from /Library/Ruby/Gems/1.8/gems/railties-3.0.3/lib/rails/engine.rb:138:in `eager_load!'
    from /Library/Ruby/Gems/1.8/gems/railties-3.0.3/lib/rails/engine.rb:137:in `each'
    from /Library/Ruby/Gems/1.8/gems/railties-3.0.3/lib/rails/engine.rb:137:in `eager_load!'
    from /Library/Ruby/Gems/1.8/gems/railties-3.0.3/lib/rails/engine.rb:135:in `each'
    from /Library/Ruby/Gems/1.8/gems/railties-3.0.3/lib/rails/engine.rb:135:in `eager_load!'
    from /Library/Ruby/Gems/1.8/gems/railties-3.0.3/lib/rails/application.rb:108:in `eager_load!'
    from /Library/Ruby/Gems/1.8/gems/railties-3.0.3/lib/rails/application/finisher.rb:41
    from /Library/Ruby/Gems/1.8/gems/railties-3.0.3/lib/rails/initializable.rb:25:in `instance_exec'
    from /Library/Ruby/Gems/1.8/gems/railties-3.0.3/lib/rails/initializable.rb:25:in `run'
    from /Library/Ruby/Gems/1.8/gems/railties-3.0.3/lib/rails/initializable.rb:50:in `run_initializers'
    from /Library/Ruby/Gems/1.8/gems/railties-3.0.3/lib/rails/initializable.rb:49:in `each'
    from /Library/Ruby/Gems/1.8/gems/railties-3.0.3/lib/rails/initializable.rb:49:in `run_initializers'
    from /Library/Ruby/Gems/1.8/gems/railties-3.0.3/lib/rails/application.rb:134:in `initialize!'
    from /Library/Ruby/Gems/1.8/gems/railties-3.0.3/lib/rails/application.rb:77:in `send'
    from /Library/Ruby/Gems/1.8/gems/railties-3.0.3/lib/rails/application.rb:77:in `method_missing'
    from /Users/habitat/Documents/RoR/tut/spec/../config/environment.rb:5
    from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
    from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in `require'
    from /Users/habitat/Documents/RoR/tut/spec/spec_helper.rb:10
    from /Library/Ruby/Gems/1.8/gems/spork-0.8.4/lib/spork.rb:23:in `prefork'
    from /Users/habitat/Documents/RoR/tut/spec/spec_helper.rb:4
    from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
    from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in `require'
    from /Users/habitat/Documents/RoR/tut/spec/controllers/pages_controller_spec.rb:1
    from /Library/Ruby/Gems/1.8/gems/rspec-core-2.1.0/lib/rspec/core/configuration.rb:334:in `load'
    from /Library/Ruby/Gems/1.8/gems/rspec-core-2.1.0/lib/rspec/core/configuration.rb:334:in `load_spec_files'
    from /Library/Ruby/Gems/1.8/gems/rspec-core-2.1.0/lib/rspec/core/configuration.rb:334:in `map'
    from /Library/Ruby/Gems/1.8/gems/rspec-core-2.1.0/lib/rspec/core/configuration.rb:334:in `load_spec_files'
    from /Library/Ruby/Gems/1.8/gems/rspec-core-2.1.0/lib/rspec/core/command_line.rb:18:in `run'
    from /Library/Ruby/Gems/1.8/gems/rspec-core-2.1.0/lib/rspec/core/runner.rb:55:in `run_in_process'
    from /Library/Ruby/Gems/1.8/gems/rspec-core-2.1.0/lib/rspec/core/runner.rb:46:in `run'
    from /Library/Ruby/Gems/1.8/gems/rspec-core-2.1.0/lib/rspec/core/runner.rb:10:in `autorun'
    from /usr/bin/rspec:19

这是user.rb代码,看起来这个问题源自..

 class User < ActiveRecord::Base
  attr_accessible :name, :email

  email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i

  validates :name,  :presence => true,
                    :length   => { :maximum => 50 }
  validates :email, :presence => true,
                    :format   => { :with => email_regex }
                    :uniqueness => { :case_sensitive => false }
end

1 个答案:

答案 0 :(得分:1)

您确定您的语法如下validates :name, :uniqueness => { :case_sensitive => false }吗?你有没有忘记','?或者使用符号以外的东西?


(编辑对应代码发布)

验证器之间需要逗号,如此

validates :email, :presence => true,
                :format   => { :with => email_regex }, # notice the comma after the hash
                :uniqueness => { :case_sensitive => false }