簡體   English   中英

語法錯誤意外在軌道上進行tANDOP 3教程

[英]Syntax Error unexpected tANDOP on rails 3 Tutorial

我認為我忽略了一些愚蠢的事情,但是我正在遵循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:in load' from /opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:469:in load_file中:639:在new_constants_in' from /opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:468:innew_constants_in' from /opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:468:innew_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 /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/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:192:in load_missing_constant 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 const_missing' from /opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:190:in每個.3 / lib / active_support / depen dencies.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 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: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 from /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中的each' from /opt/local/lib/ruby/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 get'from 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 constantize from /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中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' 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'來自/opt/local/lib/ruby/gems/1.8/gems/activerecord-3.2.3/lib/active_record/associations/collection_proxy.rb:46:in __send __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: 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 load_file中load_file' from /opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:639:in來自/opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies 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' .rb:468:在load_file' from /opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:353:inload_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:in 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 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 /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的gems / activesupport-3.2.3 / lib / active_support / dependencies.rb:514:在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 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 const_missing來自/opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/ constantize' from /opt/local/lib/ruby/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/inflector/methods.rb:217:in 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 '來自/ 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來自/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 each' from /opt/local/lib/ruby/gems/1.8/gems/activerecord-3.2.3/lib/active_record/inheritance.rb:109:in send_from 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:in 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 create_record'from /opt/local/lib/ruby/gems/1.8/gems/activerecord -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 發送 ' create' from /opt/local/lib/ruby/gems/1.8/gems/activerecord-3.2.3/lib/active_record/associations/collection_proxy.rb:46:in 'create'中

看起來有一個換行符,不應在其中:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM