2014-01-14 75 views
0
SyntaxError: C:/Users/NEBELYN/Desktop/cms/simple_cms/app/models/subject.r  
b:3: syntax error, unexpected =>, expecting ')'       
    scope :visible, lambda { where (:visible => true) }    
              ^     
C:/Users/NEBELYN/Desktop/cms/simple_cms/app/models/subject.rb:4: syntax e 
rror, unexpected =>, expecting ')'          
    scope :invisible, lambda { where (:visible => false) }   
               ^     
C:/Users/NEBELYN/Desktop/cms/simple_cms/app/models/subject.rb:10: syntax 
error, unexpected keyword_end, expecting '}'        
    from C:/Ruby200/lib/ruby/gems/2.0.0/gems/activesupport-4.0.2/lib/ 
active_support/dependencies.rb:424:in `load'        
    from C:/Ruby200/lib/ruby/gems/2.0.0/gems/activesupport-4.0.2/lib/ 
active_support/dependencies.rb:424:in `block in load_file'    
    from C:/Ruby200/lib/ruby/gems/2.0.0/gems/activesupport-4.0.2/lib/ 
active_support/dependencies.rb:616:in `new_constants_in'     

當我subject.rb中文件看起來像這樣rails4 ActiveRecord的錯誤

class Subject < ActiveRecord::Base 

scope :visible, lambda { where (:visible => true) } 
scope :invisible, lambda { where (:visible => false) } 
scope :sorted, lambda {order("subjects.position ASC")} 
scope :newest_first, lambda{ order("subjects.created_at DESC")} 
scope :search. lambda { |query| 
    where (["name like ?", "%#{query}%"]) 
} 

末 當我運行軌道控制檯「Subject.visible」這個命令請幫助

回答

1

你不能把where(之間的空格。

scope :visible, lambda { where (:visible => true) } # bad 
scope :visible, lambda { where(:visible => true) } # good 
0

where之間取出空間和open paranthesis

class Subject < ActiveRecord::Base 

scope :visible, lambda { where(:visible => true) } 
scope :invisible, lambda { where(:visible => false) } 
scope :sorted, lambda {order("subjects.position ASC")} 
scope :newest_first, lambda{ order("subjects.created_at DESC")} 
scope :search. lambda { |query| 
    where(["name like ?", "%#{query}%"]) 
}