Неожиданная синтаксическая ошибка tANDOP on rails 3 Учебное пособие

Я думаю, что упускаю из виду что-то глупое, но я следую руководству по rails 3, и я нахожусь в той части, где я добавляю ошибки, если пользователь помещает комментарий к неопубликованной статье ... Итак, в консоли rails я попробуйте создать комментарий к «неопубликованной» статье (черновику), введя следующий код

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

Здесь говорится, что я должен получить следующее:

=> #<Comment id: nil, article_id: 7, name: "Dude", email: "[email protected]", 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? ^ из /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:inload_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:inload_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:inload_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:ineach' из / 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:inload_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:ineach 'из /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:inconstantize' из / 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:inconstantize 'из / opt / local / li b / ruby ​​/ gems / 1.8 / gems / activesupport-3.2.3 / lib / active_support / dependencies.rb: 554: в get' from /opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:588:inconstantize 'из /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:ineach 'из /opt/local/lib/ruby/gems/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:insend' из /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:intransaction 'из /opt/local/lib/ruby/gems/1.8/gems/ activerecord-3.2.3 / lib / active_record / association / 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:increate 'из /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:increate '

Вот моя модель комментариев:

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:inload 'из / 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:innew_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:inrequire_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:inconst_missing 'из /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:inconst_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:inconst_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:inconst_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:ineach 'из /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:inget' из /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:incompute_type 'из /opt/local/lib/ruby/gems/1.8/gems /activerecord-3.2.3/lib/active_record/inheritance.rb:109:in each' from /opt/local/lib/ruby/gems/1.8/gems/activerecord-3.2.3/lib/active_record/inheritance.rb:109:incompute_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:inklass 'из /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:increate_record' из / opt / local / lib / ruby / gems / 1.8 / gems / activerecord-3.2.3 / lib / active_record / association / 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 send 'из /opt/local/lib/ruby/gems/1.8 /gems/activerecord-3.2.3/lib/active_record/associations/collection_proxy.rb:46:in `create '


person BennyB    schedule 06.08.2012    source источник


Ответы (1)


Похоже, есть разрыв строки там, где его не должно быть:

  def article_should_be_published
    errors.add(:article_id, "is not published yet") if article && !article.published?
  end
person iltempo    schedule 06.08.2012
comment
спасибо iltempo ... Я все же получаю новую ошибку. я включил это выше. любая идея об этом должна содержать хотя бы одну ошибку валидации. - person BennyB; 07.08.2012
comment
Nevermind ilTempo ... Я использовал проверки: когда я должен был использовать validate: ... спасибо за помощь - person BennyB; 07.08.2012