0

私は愚かなことを見落としていると思いますが、私は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:inload_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: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' from /opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:502:inload_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:ineach' from /opt/local/lib/ruby /gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:190:inconst_missing' from /opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:514:inload_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:ineach' 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:inconstantize'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:inconstantize'from /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:inconstantize' 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:ineach'from /opt/local/lib/ruby/gems/1.8/gems/activerecord-3.2.3 /lib/active_record/inheritance.rb:109:incompute_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から' /opt/local/lib/ruby/gems/1.8/gems/activerecord-3.2.3/lib/active_record/reflection.rb:172:intransaction klass' from /opt/local/lib/ruby/gems/1.8/gems/activerecord-3.2.3/lib/active_record/associations/collection_association.rb:148:in'から送信/gems/activerecord-3.2.3/lib/active_record/associations/collection_association.rb:431:in create'from 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:in __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のヒントに従った後...次の検証エラーが発生します...

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:inArgumentError:/opt/local/lib/ruby/gems/1.8/gems/activemodel-3.2.3/lib/active_model/validations/validates.rb:86:inload 'から/optから少なくとも1つの検証を提供する必要があります/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'from /opt/local/lib/ruby/gems/1.8/gems/activesupport- 3.2.3/lib/active_support/dependencies.rb:468:in load_file' from /opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:353:inrequire_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' from /opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:192:inconst_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:inconst_missing' from /opt/local/lib/ruby/gems/1.8 /gems/activesupport-3.2.3/lib/active_support/dependencies.rb:514:inload_missing_constant' from /opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:192:inconst_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:inconst_missing' from /opt/local/lib/ruby/gems/1.8 /gems/activesupport-3.2.3/lib/active_support/inflector/methods.rb:218:in each'from 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:in constantize' from /opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:554:inget'from /opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:588:incompute_type constantize' from /opt/local/lib/ruby/gems/1.8/gems/activerecord-3.2.3/lib/active_record/inheritance.rb:111:in' 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:inincompute_type'from /opt/local/lib/ruby/gems/1.8/gems/activerecord-3.2 .3 / lib / active_record /reflection.rb:172:insend' from /opt/local/lib/ruby/gems/1.8/gems/activerecord-3.2.3/lib/active_record/reflection.rb:172:inklass'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:increate_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:insend'from /opt/local/lib/ruby/gems/1.8/gems/activerecord-3.2.3/lib/ active_record / associations / collection_proxy.rb:46:in `create '

4

1 に答える 1

1

あるべきではないところに改行があるようです:

  def article_should_be_published
    errors.add(:article_id, "is not published yet") if article && !article.published?
  end
于 2012-08-06T21:02:34.530 に答える