I got the error syntax error, unexpected $end, expecting keyword_end [closed]

点点圈 提交于 2020-01-02 10:04:55

问题


I'm first to ruby, so when i following the Guide(http://guides.rubyonrails.org/getting_started.html) I got the error like:

Started GET "/questions" for 127.0.0.1 at 2012-06-07 17:22:36 +0900

SyntaxError (/Users/sookcha/Desktop/Dev/CSap/app/models/question.rb:3: invalid multibyte char (US-ASCII) /Users/sookcha/Desktop/Dev/CSap/app/models/question.rb:3: syntax error, unexpected $end, expecting keyword_end   validates :name,  :presence => true ^): app/controllers/questions_controller.rb:2:in `'

Rendered /Users/sookcha/.rvm/gems/ruby-1.9.3-p125/gems/actionpack-3.2.5/lib/action_dispatch/middleware/templates/rescues/_trace.erb (3.1ms) Rendered /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) Rendered /Users/sookcha/.rvm/gems/ruby-1.9.3-p125/gems/actionpack-3.2.5/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (10.7ms)

when I refresh the page, i got a different error below:

Started GET "/questions" for 127.0.0.1 at 2012-06-07 17:22:40 +0900

NoMethodError (undefined method `key?' for nil:NilClass):

My code is

class Question < ActiveRecord::Base
attr_accessible :content, :name, :title
validates :name,  :presence => true
validates :title, :presence => true,
                  :length => { :minimum => 5 }

end

What's wrong with my code?

EDIT

When I insert # encoding: utf-8, in my code, I got a different error like: /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 } ^):
What's wrong?


回答1:


There's a non-breaking space in your code:

validates :name,  :presence => true
                ^
               here



回答2:


Your code is fine, I bet your editor is inserting a non-standard tab somewhere. Place this at the top of your model file:

# encoding: utf-8

It's a magic comment that instructs ruby to interpret any non-ascii strings or literals as UTF-8.



来源:https://stackoverflow.com/questions/10928310/i-got-the-error-syntax-error-unexpected-end-expecting-keyword-end

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!