Rails 3 NoMethodError未定义方法`name'

时间:2012-02-06 09:27:35

标签: ruby-on-rails-3

当我在评论对象上调用comment.save时,我收到错误,有任何帮助调试吗?

这是输出:

NoMethodError: undefined method `name' for #<Comment:0x107b4c280>
    from /Library/Ruby/Gems/1.8/gems/activemodel-3.0.5/lib/active_model/attribute_methods.rb:364:in `method_missing'
    from /Library/Ruby/Gems/1.8/gems/activerecord-3.0.5/lib/active_record/attribute_methods.rb:46:in `method_missing'
    from /Library/Ruby/Gems/1.8/gems/activemodel-3.0.5/lib/active_model/errors.rb:224:in `read_attribute_for_validation'
    from /Library/Ruby/Gems/1.8/gems/activemodel-3.0.5/lib/active_model/errors.rb:224:in `send'
    from /Library/Ruby/Gems/1.8/gems/activemodel-3.0.5/lib/active_model/errors.rb:224:in `add_on_blank'
    from /Library/Ruby/Gems/1.8/gems/activemodel-3.0.5/lib/active_model/errors.rb:223:in `each'
    from /Library/Ruby/Gems/1.8/gems/activemodel-3.0.5/lib/active_model/errors.rb:223:in `add_on_blank'
    from /Library/Ruby/Gems/1.8/gems/activemodel-3.0.5/lib/active_model/validations/presence.rb:9:in `validate'
    from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.5/lib/active_support/callbacks.rb:314:in `send'
    from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.5/lib/active_support/callbacks.rb:314:in `_callback_before_1143'
    from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.5/lib/active_support/callbacks.rb:414:in `_run_validate_callbacks'
    from /Library/Ruby/Gems/1.8/gems/activemodel-3.0.5/lib/active_model/validations.rb:212:in `run_validations!'
    from /Library/Ruby/Gems/1.8/gems/activemodel-3.0.5/lib/active_model/validations/callbacks.rb:67:in `run_validations!'
    from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.5/lib/active_support/callbacks.rb:413:in `_run_validation_callbacks'
    from /Library/Ruby/Gems/1.8/gems/activemodel-3.0.5/lib/active_model/validations/callbacks.rb:67:in `run_validations!'
    from /Library/Ruby/Gems/1.8/gems/activemodel-3.0.5/lib/active_model/validations.rb:179:in `valid?'
    from /Library/Ruby/Gems/1.8/gems/activerecord-3.0.5/lib/active_record/validations.rb:55:in `valid?'
    from /Library/Ruby/Gems/1.8/gems/activerecord-3.0.5/lib/active_record/validations.rb:75:in `perform_validations'
    from /Library/Ruby/Gems/1.8/gems/activerecord-3.0.5/lib/active_record/validations.rb:43:in `save'
    from /Library/Ruby/Gems/1.8/gems/activerecord-3.0.5/lib/active_record/attribute_methods/dirty.rb:21:in `save'
    from /Library/Ruby/Gems/1.8/gems/activerecord-3.0.5/lib/active_record/transactions.rb:240:in `save'
    from /Library/Ruby/Gems/1.8/gems/activerecord-3.0.5/lib/active_record/transactions.rb:292:in `with_transaction_returning_status'
    from /Library/Ruby/Gems/1.8/gems/activerecord-3.0.5/lib/active_record/connection_adapters/abstract/database_statements.rb:139:in `transaction'
    from /Library/Ruby/Gems/1.8/gems/activerecord-3.0.5/lib/active_record/transactions.rb:207:in `transaction'
    from /Library/Ruby/Gems/1.8/gems/activerecord-3.0.5/lib/active_record/transactions.rb:290:in `with_transaction_returning_status'
    from /Library/Ruby/Gems/1.8/gems/activerecord-3.0.5/lib/active_record/transactions.rb:240:in `save'
    from /Library/Ruby/Gems/1.8/gems/activerecord-3.0.5/lib/active_record/transactions.rb:251:in `rollback_active_record_state!'
    from /Library/Ruby/Gems/1.8/gems/activerecord-3.0.5/lib/active_record/transactions.rb:239:in `save'
    from (irb):645
    from :0>> 

...

运行后会发生这种情况:

c = event.comment.new
c.body = "test comment"
c.save

我已经阅读了很多线程,说这是Rails版本和Rubygems版本的问题,但我发现很难相信。

1 个答案:

答案 0 :(得分:6)

以下其中一项:

  1. 您有一个显示或索引视图,其名称为comment.name
  2. 您在评论模型中对名称进行了验证
  3. 但很难从你提供的信息中分辨出来。你在哪里得到错误?