ruby-on-rails - 语法错误意外的tANDOP on rails 3教程

标签 ruby-on-rails ruby-on-rails-3 syntax-error

我认为我忽略了一些愚蠢的事情,但是我正在遵循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"]

但是相反,我收到以下错误:

语法错误:/Users/bbarton250/Sites/rails_projects/theoldman/app/models/comment.rb:11:语法错误,意外的tANDOP,期望kEND
&&!article.published?
^
来自/opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:469:在load' from /opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:469:in load_file中'
来自/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 load_file中'
来自/opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:353:在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中每个
来自/opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:190:在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/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中每个
来自/opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:190:在const_missing' from /opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/inflector/methods.rb:218:in中常数化'
来自/opt/local/lib/ruby/gems/1.8/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 get' from /opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:588:in constantize'
来自/opt/local/lib/ruby/gems/1.8/gems/activerecord-3.2.3/lib/active_record/inheritance.rb:111:在compute_type' 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/inheritance.rb:109:在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:在klass' from /opt/local/lib/ruby/gems/1.8/gems/activerecord-3.2.3/lib/active_record/associations/collection_association.rb:148:in交易中'
来自/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中创建'
来自/opt/local/lib/ruby/gems/1.8/gems/activerecord-3.2.3/lib/active_record/associations/collection_proxy.rb:46:在__send__' from /opt/local/lib/ruby/gems/1.8/gems/activerecord-3.2.3/lib/active_record/associations/collection_proxy.rb:46:in中创建'

这是我的评论模型:
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:在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'
来自/opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:468:在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中
来自/opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:502:在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中
来自/opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:190:在each' from /opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:190:in const_missing中
来自/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 const_missing'
来自/opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:190:在each' from /opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:190:in const_missing中
来自/opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/inflector/methods.rb:218:每个constantize' 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/inflector/methods.rb:217:在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:在constantize' from /opt/local/lib/ruby/gems/1.8/gems/activerecord-3.2.3/lib/active_record/inheritance.rb:111:in compute_type中
来自/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的compute_type中
来自/opt/local/lib/ruby/gems/1.8/gems/activerecord-3.2.3/lib/active_record/reflection.rb:172:在send' 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/associations/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 create_record中
来自/opt/local/lib/ruby/gems/1.8/gems/activerecord-3.2.3/lib/active_record/associations/collection_association.rb:119:在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:在'create'中

最佳答案

看起来有一个换行符,不应在其中:

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

关于ruby-on-rails - 语法错误意外的tANDOP on rails 3教程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11835848/

相关文章:

ruby-on-rails - 工作流或 AASM 等 gem 的最佳实践

rake - Rake 任务中未初始化的常量

php - mysql where子句中的未知列

python - 这些 PostgreSQL 查询之间的区别和修复错误的查询?

python - 评估赋值时出现语法错误

ruby-on-rails - 关闭 Rails 中的 "updated_at"列

ruby-on-rails - AngularJS 向 API 发送 nil 参数

ruby-on-rails - rails 5 : Adding conditional custom validations

ruby-on-rails - 如何构建/安装 Travis Build?

ruby-on-rails - Ruby on Rails 分页 : Sort order not respected (will_paginate gem)