您好我是ruby on rails平台的初学者,我正在阅读railstutorial.org书。我的Micopost1.rb文件内容是
class Micropost1 < ActiveRecord::Base
attr accessible :content, :user_id
belongs_to :user
validates :content, :length => { :maximum => 140 }
end
当我运行rails.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是User,第二个是Micropost1 首先,我运行此命令来分配变量first_user first_user = User.first 请指出我错误的地方。
答案 0 :(得分: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.rb文件的第2行,有一个不存在的方法被调用。
答案 1 :(得分:2)
试试这个......
class Micropost1 < ActiveRecord::Base
attr_accessible :content, :user_id
belongs_to :user
validates :content, :length => { :maximum => 140 }
end
你在模型中写的是'attr accessible', 就像上面写的那样'attr_accessible'......
答案 2 :(得分: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
是什么意思?谷歌是你的沃森。他给你6 630个不同的答案。
结案
您发现自己在模型中写的是'attr accessible'
,而应该是'attr_accessible'
。