开发者

Rails 3 :uniqueness option spitting out syntax error

I'm trying to figure out why this uniqueness option keeps failing and have exhausted all the options. Anybody have any idea why this might be erroring out in rspec?

Many thanks,

$ 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_开发者_StackOverflow中文版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

Here is the user.rb code which it looks like this issue is stemming from..

 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


Are you certain your syntax looks like this validates :name, :uniqueness => { :case_sensitive => false } ? Did you maybe forget the ',' ? Or use something other than symbols ?


(edit corresponding to code posting)

You need a comma between the validators, like so

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜