0

嗨,我是 rails 平台上的 ruby​​ 初学者,我正在阅读 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 是用户,第二个是 Micropost1 首先我运行这个命令来分配变量 first_user first_user=User.first 请指出我在哪里弄错了。

4

3 回答 3

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 行,调用了一个不存在的方法。

于 2012-12-17T14:00:28.277 回答
2

尝试这个...

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

  belongs_to :user

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

end

你在你的模型中写的是'attr accessible',它是'attr_accessible',就像上面写的一样......

于 2012-12-17T14:05:20.707 回答
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意思?谷歌是你的沃森。他给你6630个不同的答案。

结案

你发现你在你的模型中写的是 is 'attr accessible',而不是应该是'attr_accessible'

于 2012-12-17T16:04:52.567 回答