2012-12-17 79 views
0

嗨,我在軌道上平臺紅寶石初學者,我會通過railstutorial.org book.My Micopost1.rb文件內容語法錯誤在Ruby on Rails的

class Micropost1 < ActiveRecord::Base 
    attr accessible :content, :user_id 

belongs_to :user 

validates :content, :length => { :maximum => 140 } 

end 

,當我運行軌道.console命令,然後在控制檯打開,當我在控制檯 第一user.microposts類型,然後我得到下面的錯誤日誌,

SyntaxError: /home/ritesh/projects/demo/app/models/micropost1.rb:2: syntax error, unexpected tIDENTIFIER, expecting kEND 
    from /var/lib/gems/1.8/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:469:in `load' 
    from /var/lib/gems/1.8/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:469:in `load_file' 
    from /var/lib/gems/1.8/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:639:in `new_constants_in' 
    from /var/lib/gems/1.8/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:468:in `load_file' 
    from /var/lib/gems/1.8/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:353:in `require_or_load' 
    from /var/lib/gems/1.8/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:502:in `load_missing_constant' 
    from /var/lib/gems/1.8/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:192:in `const_missing' 
    from /var/lib/gems/1.8/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:190:in `each' 
    from /var/lib/gems/1.8/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:190:in `const_missing' 
    from /var/lib/gems/1.8/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:514:in `load_missing_constant' 
    from /var/lib/gems/1.8/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:192:in `const_missing' 
    from /var/lib/gems/1.8/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:190:in `each' 
    from /var/lib/gems/1.8/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:190:in `const_missing' 
    from /var/lib/gems/1.8/gems/activesupport-3.2.9/lib/active_support/inflector/methods.rb:219:in `constantize' 
    from /var/lib/gems/1.8/gems/activesupport-3.2.9/lib/active_support/inflector/methods.rb:218:in `each' 
    from /var/lib/gems/1.8/gems/activesupport-3.2.9/lib/active_support/inflector/methods.rb:218:in `constantize' 
... 10 levels... 
    from /var/lib/gems/1.8/gems/activerecord-3.2.9/lib/active_record/associations/collection_proxy.rb:44:in `__send__' 
    from /var/lib/gems/1.8/gems/activerecord-3.2.9/lib/active_record/associations/collection_proxy.rb:44:in `load_target' 
    from /var/lib/gems/1.8/gems/activerecord-3.2.9/lib/active_record/associations/collection_proxy.rb:87:in `method_missing' 
    from /usr/lib/ruby/1.8/irb.rb:310:in `output_value' 
    from /usr/lib/ruby/1.8/irb.rb:159:in `eval_input' 
    from /usr/lib/ruby/1.8/irb.rb:271:in `signal_status' 
    from /usr/lib/ruby/1.8/irb.rb:155:in `eval_input' 
    from /usr/lib/ruby/1.8/irb.rb:154:in `eval_input' 
    from /usr/lib/ruby/1.8/irb.rb:71:in `start' 
    from /usr/lib/ruby/1.8/irb.rb:70:in `catch' 
    from /usr/lib/ruby/1.8/irb.rb:70:in `start' 
    from /var/lib/gems/1.8/gems/railties-3.2.9/lib/rails/commands/console.rb:47:in `start' 
    from /var/lib/gems/1.8/gems/railties-3.2.9/lib/rails/commands/console.rb:8:in `start' 
    from /var/lib/gems/1.8/gems/railties-3.2.9/lib/rails/commands.rb:41 
    from script/rails:6:in `require' 
    from script/rails:6irb(main):004:0> first_user.micropost1s 
NoMethodError: undefined method `accessible' for #<Class:0xb6651934> 
    from /var/lib/gems/1.8/gems/activerecord-3.2.9/lib/active_record/dynamic_matchers.rb:50:in `method_missing' 
    from /home/ritesh/projects/demo/app/models/micropost1.rb:2 
    from /var/lib/gems/1.8/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:469:in `load' 
    from /var/lib/gems/1.8/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:469:in `load_file' 
    from /var/lib/gems/1.8/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:639:in `new_constants_in' 
    from /var/lib/gems/1.8/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:468:in `load_file' 
    from /var/lib/gems/1.8/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:353:in `require_or_load' 
    from /var/lib/gems/1.8/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:502:in `load_missing_constant' 
    from /var/lib/gems/1.8/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:192:in `const_missing' 
    from /var/lib/gems/1.8/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:190:in `each' 
    from /var/lib/gems/1.8/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:190:in `const_missing' 
    from /var/lib/gems/1.8/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:514:in `load_missing_constant' 
    from /var/lib/gems/1.8/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:192:in `const_missing' 
    from /var/lib/gems/1.8/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:190:in `each' 
    from /var/lib/gems/1.8/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:190:in `const_missing' 
    from /var/lib/gems/1.8/gems/activesupport-3.2.9/lib/active_support/inflector/methods.rb:219:in `constantize' 
... 12 levels... 
    from /var/lib/gems/1.8/gems/activerecord-3.2.9/lib/active_record/associations/collection_proxy.rb:44:in `__send__' 
    from /var/lib/gems/1.8/gems/activerecord-3.2.9/lib/active_record/associations/collection_proxy.rb:44:in `load_target' 
    from /var/lib/gems/1.8/gems/activerecord-3.2.9/lib/active_record/associations/collection_proxy.rb:87:in `method_missing' 
    from /usr/lib/ruby/1.8/irb.rb:310:in `output_value' 
    from /usr/lib/ruby/1.8/irb.rb:159:in `eval_input' 
    from /usr/lib/ruby/1.8/irb.rb:271:in `signal_status' 
    from /usr/lib/ruby/1.8/irb.rb:155:in `eval_input' 
    from /usr/lib/ruby/1.8/irb.rb:154:in `eval_input' 
    from /usr/lib/ruby/1.8/irb.rb:71:in `start' 
    from /usr/lib/ruby/1.8/irb.rb:70:in `catch' 
    from /usr/lib/ruby/1.8/irb.rb:70:in `start' 
    from /var/lib/gems/1.8/gems/railties-3.2.9/lib/rails/commands/console.rb:47:in `start' 
    from /var/lib/gems/1.8/gems/railties-3.2.9/lib/rails/commands/console.rb:8:in `start' 
    from /var/lib/gems/1.8/gems/railties-3.2.9/lib/rails/commands.rb:41 
    from script/rails:6:in `require' 

,我沒有得到我的錯誤有兩種模式1是用戶和第二是Micropost1 首先我運行這個命令來分配var first_user first_user = User.first 請指出我誤解的地方。

回答

3

attr accessible應該是一個字:attr_accessible

如果您在堆棧跟蹤往下看,你會看到以下錯誤:

NoMethodError: undefined method `accessible' for #<Class:0xb6651934> 
    from /var/lib/gems/1.8/gems/activerecord-3.2.9/lib/active_record/dynamic_matchers.rb:50:in `method_missing' 
    from /home/ritesh/projects/demo/app/models/micropost1.rb:2 

此錯誤表明您micropost1的2線。 rb文件,有一種方法被稱爲不存在。

2

嘗試......

class Micropost1 < ActiveRecord::Base 
    attr_accessible :content, :user_id 

    belongs_to :user 

    validates :content, :length => { :maximum => 140 } 

end 

你已經寫在你的模式是「ATTR訪問」, 是「attr_accessible」像上面寫的是什麼...

+0

在沒有任何解釋的情況下發布代碼片段並不能真正幫助未來的訪問者。指出爲什麼你提出這個建議是很有用的,這樣如果有人用類似的但不是確切的問題絆倒了這個問題,他們會有更多的信息來解決這個問題。 –

+0

感謝您指出這樣的錯誤.......我會盡快修復。 – Cyber

+1

這不是一個錯誤,只是需要改進。可以簡要說明,但提供的背景越多,未來的訪問者就越有幫助。這裏有一些其他技巧:http://stackoverflow.com/questions/how-to-answer –

0

你應該做一些調查,假裝你是福爾摩斯!

線索1

SyntaxError: /home/ritesh/projects/demo/app/models/micropost1.rb:2: syntax error, unexpected tIDENTIFIER, expecting kEND 

你知道你有什麼樣的錯誤,語法錯誤。你拼錯了一些東西。

線索2

如果你仔細觀察,你可以看到,我們有其他線索,那就是錯誤告訴你,它是在模型micropost1.rb:2:2意味着它是在第二行中的碼。

線索3

歐凱,你知道哪裏出錯,這是什麼樣的錯誤。但是unexpected tIDENTIFIER, expecting kEND是什麼意思? Google是你的華生。他給你6 630個不同的答案。

結案

你發現,你已經寫在你的模型是'attr accessible',相反,它應該是'attr_accessible'