我正在阅读一个教程,并在尝试将用户添加到我的数据库时想出了这个错误语句。我正在试图弄清楚如何正确阅读这些错误陈述。例如,我看到输出显示语法错误,意外的tASSOC。期待keyword_end。所以为了在发布之前尝试解决这个问题,我在user.rb文件中注释掉了这一行(语句指向的那一行)。但那没用。我还添加了一个额外的“结束”,因为终端输出表示“期待keyword_end”。
以下是确切的终端输出:
SyntaxError: /Users/zkidd/Sites/rails_projects/sample_app/app/models/user.rb:19: syntax error, unexpected tASSOC, expecting keyword_end
:length => { :maximum => 50 }
^
/Users/zkidd/Sites/rails_projects/sample_app/app/models/user.rb:23: syntax error, unexpected tASSOC, expecting $end
:format => { :with => email_regex },
^
from /Users/zkidd/.rvm/gems/ruby-1.9.2-p0@rails3tutorial/gems/activesupport-3.0.1/lib/active_support/dependencies.rb:454:in `load'
from /Users/zkidd/.rvm/gems/ruby-1.9.2-p0@rails3tutorial/gems/activesupport-3.0.1/lib/active_support/dependencies.rb:454:in `block in load_file'
from /Users/zkidd/.rvm/gems/ruby-1.9.2-p0@rails3tutorial/gems/activesupport-3.0.1/lib/active_support/dependencies.rb:591:in `new_constants_in'
from /Users/zkidd/.rvm/gems/ruby-1.9.2-p0@rails3tutorial/gems/activesupport-3.0.1/lib/active_support/dependencies.rb:453:in `load_file'
from /Users/zkidd/.rvm/gems/ruby-1.9.2-p0@rails3tutorial/gems/activesupport-3.0.1/lib/active_support/dependencies.rb:340:in `require_or_load'
from /Users/zkidd/.rvm/gems/ruby-1.9.2-p0@rails3tutorial/gems/activesupport-3.0.1/lib/active_support/dependencies.rb:491:in `load_missing_constant'
from /Users/zkidd/.rvm/gems/ruby-1.9.2-p0@rails3tutorial/gems/activesupport-3.0.1/lib/active_support/dependencies.rb:183:in `block in const_missing'
from /Users/zkidd/.rvm/gems/ruby-1.9.2-p0@rails3tutorial/gems/activesupport-3.0.1/lib/active_support/dependencies.rb:181:in `each'
from /Users/zkidd/.rvm/gems/ruby-1.9.2-p0@rails3tutorial/gems/activesupport-3.0.1/lib/active_support/dependencies.rb:181:in `const_missing'
from (irb):6
from /Users/zkidd/.rvm/gems/ruby-1.9.2-p0@rails3tutorial/gems/railties-3.0.1/lib/rails/commands/console.rb:44:in `start'
from /Users/zkidd/.rvm/gems/ruby-1.9.2-p0@rails3tutorial/gems/railties-3.0.1/lib/rails/commands/console.rb:8:in `start'
from /Users/zkidd/.rvm/gems/ruby-1.9.2-p0@rails3tutorial/gems/railties-3.0.1/lib/rails/commands.rb:23:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
这是我的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
任何指导都会受到赞赏,因为我真的不想从头开始重建这个东西: - )
答案 0 :(得分:5)
在以下情况之后您缺少逗号:presence =&gt;真实参数:
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, :presense => true,
:format => { :with => email_regex },
:uniqueness => { :case_sensitive => false }
end
答案 1 :(得分:0)
语法错误之前的2行上有一些缺少的逗号。