gpt4 book ai didi

ruby-on-rails - Rails 3 :uniqueness option spitting out syntax error

转载 作者:行者123 更新时间:2023-12-03 08:31:57 24 4
gpt4 key购买 nike

我试图弄清楚为什么该唯一性选项会不断失败并耗尽所有选项。有人知道为什么这会在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

最佳答案

您确定语法类似于validates :name, :uniqueness => { :case_sensitive => false }吗?您是否可能忘记了“,”?或者使用除符号以外的其他符号?

(对应于代码发布的编辑)

验证者之间需要逗号,就像这样

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

关于ruby-on-rails - Rails 3 :uniqueness option spitting out syntax error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4295774/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com