我是第一个使用ruby,所以当我按照指南(http://guides.rubyonrails.org/getting_started.html)时,我得到的错误如下:
在2012-06-07 17:22:36 +0900
开始获取127.0.0.1的“/问题”SyntaxError(/Users/sookcha/Desktop/Dev/CSap/app/models/question.rb:3:无效的多字节字符(US-ASCII) /Users/sookcha/Desktop/Dev/CSap/app/models/question.rb:3:语法错误,意外$ end,期待keyword_end 验证:name,:presence =>真正 ^): app / controllers / questions_controller.rb:2:在`'
呈现/Users/sookcha/.rvm/gems/ruby-1.9.3-p125/gems/actionpack-3.2.5/lib/action_dispatch/middleware/templates/rescues/_trace.erb(3.1ms) 呈现/Users/sookcha/.rvm/gems/ruby-1.9.3-p125/gems/actionpack-3.2.5/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb(1.1ms) 在救援/布局(10.7ms)内呈现/Users/sookcha/.rvm/gems/ruby-1.9.3-p125/gems/actionpack-3.2.5/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb
当我刷新页面时,我在下面遇到了不同的错误:
Started GET "/questions" for 127.0.0.1 at 2012-06-07 17:22:40 +0900
NoMethodError (undefined method `key?' for nil:NilClass):
我的代码是
class Question < ActiveRecord::Base
attr_accessible :content, :name, :title
validates :name, :presence => true
validates :title, :presence => true,
:length => { :minimum => 5 }
end
我的代码出了什么问题?
修改
当我插入时
我的代码中# encoding: utf-8,
,
我得到了一个不同的错误:
/Users/sookcha/Desktop/Dev/CSap/app/models/question.rb:5: syntax error, unexpected tSYMBEG, expecting keyword_do or '{' or '(' validates :name, :presence => true ^ /Users/sookcha/Desktop/Dev/CSap/app/models/question.rb:7: syntax error, unexpected tASSOC, expecting keyword_end :length => { :minimum => 5 } ^):
怎么了?
答案 0 :(得分:4)
你的代码很好,我打赌你的编辑器正在某处插入一个非标准的标签。将其放在模型文件的顶部:
# encoding: utf-8
这是一个神奇的评论,指示ruby将任何非ascii字符串或文字解释为UTF-8。
答案 1 :(得分:4)
代码中有non-breaking space:
validates :name, :presence => true
^
here