【问题标题】:How to solve argument error in Rails?如何解决 Rails 中的参数错误?
【发布时间】:2014-06-01 10:35:10
【问题描述】:

我正在使用 Ruby 1.9.3 和 Rails 3.2.16。我正在阅读 Michael hartl 的 Rails 教程的第 10 章。

当我运行bundle exec rspec spec/ 时出现以下错误:

/home/james/.rvm/gems/ruby-1.9.3-p484@rails3/gems/activemodel-3.2.16/lib/active_model/validations/validates.rb:86:in `validates': You need to supply at least one validation (ArgumentError)

from /home/james/rails3.2/rails_projects/sample_app/app/models/micropost.rb:7:in `<class:Micropost>'

我的 micropost.rb 文件

class Micropost < ActiveRecord::Base
  attr_accessible :content

  belongs_to :user

  validates :content, presence: true, length: { maximum: 140 }
  validates :user_id

  default_scope order: 'microposts.created_at DESC'
end

我该如何解决这个错误?

谢谢。

【问题讨论】:

    标签: ruby-on-rails-3 rspec


    【解决方案1】:

    错误本身为解决此错误提供了线索。

    在您的 micropost.rb 文件中,在 validates :user_id 之后添加 presence: true 代码。

    您更新后的 micropost.rb 文件如下所示:

    class Micropost < ActiveRecord::Base
      attr_accessible :content
    
      belongs_to :user
    
      validates :content, presence: true, length: { maximum: 140 }
      validates :user_id, presence: true
    
      default_scope order: 'microposts.created_at DESC'
    end
    

    我认为这将解决您的错误。

    【讨论】:

    • 哦!我找不到。太丢人了。
    猜你喜欢
    • 2017-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-15
    • 1970-01-01
    • 2019-12-23
    • 2013-02-27
    • 2021-12-26
    相关资源
    最近更新 更多