在rails 3教程中,语法错误意外的tANDOP

我认为我忽略了一些愚蠢的东西,但是我遵循了rails 3的教程,而且如果用户对未发布的文章发表评论,那么我会在其中添加错误...因此,在rails控制台中,我尝试通过输入以下代码在“未发布”的文章(草稿)上创建评论

>> article = Article.draft.first
=> #<Article id: 7, title: "One-to-many associations",        ...> 
>> comment = article.comments.create :name => 'Dude', :email => 'dude@example.com', :body    => 'Great article!'

在这一点上它说我应该得到以下内容:

=> #<Comment id: nil, article_id: 7, name: "Dude", email: "dude@example.com", body: "Great article!", created_at: nil, updated_at: nil> >> comment.errors.full_messages => ["Article is not published yet"]

但是,我得到以下错误:

SyntaxError:/Users/bbarton250/Sites/rails_projects/theoldman/app/models/comment.rb:11:语法错误,意外的tANDOP,期待kEND &&!article.published? ^ from /opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:469:in load' from /opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:469:in load_file'from /opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb :639:在new_constants_in' from /opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:468:in从/ opt / local / lib / ruby new_constants_in' from /opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:468:in中的new_constants_in' from /opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:468:in /gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:353:in require_or_load' from /opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:502:in /opt/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:192:in const_missing' from /opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:190:in require_or_load' from /opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:502:in load_missing_constant中的/opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:192 const_missing' from /opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:190:in from /opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2 0.3 / LIB / active_support /依赖新生 const_missing' from /opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:514:in load_missing_constant'从/ opt / local /目录中的dencies.rb:190:在const_missing' from /opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:514:in lib / ruby​​ / gems / 1.8 / gems / activesupport-3.2.3 / lib / active_support / dependencies.rb:192:在const_missing' from /opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:190:in from /opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:190:in const_missing' from /opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/inflector/methods.rb:218:in constantize' gems / activesupport-3.2.3 / lib / active_support / inflector / methods.rb:217:在each' from /opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/inflector/methods.rb:217:in “从/opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:554:in constantize get' from /opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:588:in get' from /opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:588:in /opt/local/lib/ruby/gems/1.8/gems/activerecord-3.2.3/lib/active_record/inheritance.rb:111中的“ get' from /opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:588:in constantize中” :in compute_type' from /opt/local/lib/ruby/gems/1.8/gems/activerecord-3.2.3/lib/active_record/inheritance.rb:109:in from / opt / local / lib / ruby​​ / gems中的compute_type' from /opt/local/lib/ruby/gems/1.8/gems/activerecord-3.2.3/lib/active_record/inheritance.rb:109:in /1.8/gems/activerecord-3.2.3/lib/active_record/inheritance.rb:109:in compute_type' from /opt/local/lib/ruby/gems/1.8/gems/activerecord-3.2.3/lib/active_record/reflection.rb:172:in从/opt/local/lib/ruby/gems/1.8/gems/activerecord-3.2.3/lib/active_record/reflection.rb:172:in klass'in klass' from /opt/local/lib/ruby/gems/1.8/gems/activerecord-3.2.3/lib/active_record/associations/collection_association.rb:148:in transaction'from /opt/local/lib/ruby/gems/1.8/gems/activerecord-3.2 .3 / lib / active_record / associations / collection_association.rb:431:在create_record' from /opt/local/lib/ruby/gems/1.8/gems/activerecord-3.2.3/lib/active_record/associations/collection_association.rb:119:in创造 e __send__' from /opt/local/lib/ruby/gems/1.8/gems/activerecord-3.2.3/lib/active_record/associations/collection_proxy.rb:46:in activerecord- __send__' from /opt/local/lib/ruby/gems/1.8/gems/activerecord-3.2.3/lib/active_record/associations/collection_proxy.rb:46:in create'

这是我的评论模型:

class Comment < ActiveRecord::Base
  attr_accessible :body, :email, :name, :article_id

  belongs_to :article

  validates :name, :email, :body, :presence => true
  validates :article_should_be_published

  def article_should_be_published
    errors.add(:article_id, "is not published yet") if article 
&& !article.published?
  end
end

这是我的文章模型:

class Article < ActiveRecord::Base
  attr_accessible :body, :published_at, :title

  validates :title, :presence => true
  validates :body, :presence => true

  belongs_to :user
  has_and_belongs_to_many :categories
  has_many :comments

  scope :published, where("articles.published_at IS NOT NULL")
  scope :draft, where("articles.published_at IS NULL")
  # recent post from < a month ago - see pg 103 of tutorial
  # scope :recent, lambda { published.where("articles.published_at > ?", 1.month.ago.to_date)}
  scope :where_title, lambda { |term| where("articles.title LIKE ?", "%#{term}%") }

  def long_title
    "#{title} - #{published_at}"
  end

  def published?
    published_at.present?
  end
end

请让我知道如果我需要提供其他任何东西...我感谢任何和所有帮助...非常感谢

更新:

在遵循iltempos提示之后...我现在正在收到以下验证错误...

ArgumentError:您需要从/opt/local/lib/ruby/gems/1.8/gems/activemodel-3.2.3/lib/active_model/validations/validates.rb:86:in validates' from /Users/bbarton250/Sites/rails_projects/theoldman/app/models/comment.rb:7 from /opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:469:in至少一个验证validates' from /Users/bbarton250/Sites/rails_projects/theoldman/app/models/comment.rb:7 from /opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:469:in '从/opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:469:in负载load_file' from /opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:639:in new_constants_in'from /opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies .rb:468:in / opt / local / lib中的load_file' from /opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:353:in require_or_load'中的load_file' from /opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:353:in '中/opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2中的load_missing_constant' from /opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:192:in load_missing_constant' from /opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:192:in const_missing'from /opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:190:in each' from /opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:190:in const_missing'from /opt/local/lib/ruby/gems/1.8/ gems / activesupport-3.2.3 / lib / active_support / dependencies.rb:514:in load_missing_constant' from /opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:192:in each' from /opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:190:in load_missing_constant' from /opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:192:in /opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:190:in' load_missing_constant' from /opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:192:in const_missing'中each' from /opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:190:in const_missing'from /opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/ active_support / inflector / methods.rb:218:in constantize' from /opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/inflector/methods.rb:217:in each'从/ opt / local / lib / rub y / gems / 1.8 / gems / activesupport-3.2.3 / lib / active_support / inflector / methods.rb:217:in constantize' from /opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:554:in从/opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:588:in constantize' from /opt/local/lib/ruby/gems/1.8/gems/activerecord-3.2.3/lib/active_record/inheritance.rb:111:in compute_type'from /opt/local/lib/ruby/gems/1.8/gems/ activerecord-3.2.3 / lib / active_record / inheritance.rb:109:在each' from /opt/local/lib/ruby/gems/1.8/gems/activerecord-3.2.3/lib/active_record/inheritance.rb:109:in /opt/local/lib/ruby/gems/1.8/gems/activerecord-3.2.3/lib/active_record/reflection.rb:172:in send' from /opt/local/lib/ruby/gems/1.8/gems/activerecord-3.2.3/lib/active_record/reflection.rb:172:in each' from /opt/local/lib/ruby/gems/1.8/gems/activerecord-3.2.3/lib/active_record/inheritance.rb:109:in compute_type' send' from /opt/local/lib/ruby/gems/1.8/gems/activerecord-3.2.3/lib/active_record/reflection.rb:172:in klass'from /opt/local/lib/ruby/gems/1.8/gems/activerecord-3.2.3/lib/active_record/关联/ collection_association.rb:148:在transaction' from /opt/local/lib/ruby/gems/1.8/gems/activerecord-3.2.3/lib/active_record/associations/collection_association.rb:431:in transaction' from /opt/local/lib/ruby/gems/1.8/gems/activerecord-3.2.3/lib/active_record/associations/collection_association.rb:431:in /opt/local/lib/ruby/gems/1.8/gems/activerecord中的create_record中-3.2.3 / lib / active_record / associations / collection_association.rb:119:in / create' from /opt/local/lib/ruby/gems/1.8/gems/activerecord-3.2.3/lib/active_record/associations/collection_proxy.rb:46:in 发送 “从/opt/local/lib/ruby/gems/1.8/gems/activerecord-3.2.3/lib/active_record/associations/collection_proxy.rb:46:in'创建”


看起来有一个换行符,它不应该是:

  def article_should_be_published
    errors.add(:article_id, "is not published yet") if article && !article.published?
  end
链接地址: http://www.djcxy.com/p/81121.html

上一篇: Syntax Error unexpected tANDOP on rails 3 Tutorial

下一篇: RoR: ActiveRecord, NoMethodError in models that are extended from AR