I think i'm overlooking something stupid but i'm following a rails 3 tutorial, and I'm at the part where I'm adding errors if a user places a comment on an unpublished article...So in the rails console I try to create a comment on an "unpublished" article (a draft) by entering the following code
>> article = Article.draft.first
=> #<Article id: 7, title: "One-to-many associations", ...>
>> comment = article.comments.create :name => 'Dude', :email => '[email protected]', :body => 'Great article!'
At this point it says i'm supposed to get the following:
=> #<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"]
But instead I'm getting the following error:
SyntaxError: /Users/bbarton250/Sites/rails_projects/theoldman/app/models/comment.rb:11: syntax error, unexpected tANDOP, expecting 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:in 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'
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: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 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/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'
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 /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:in
each'
from /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:in
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/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 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 __send__'
from /opt/local/lib/ruby/gems/1.8/gems/activerecord-3.2.3/lib/active_record/associations/collection_proxy.rb:46:in
create'
Here is my comment model:
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
Here is my article model:
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
Please let me know if i need to provide anything else...I appreciate any and all help...Thanks so much
UPDATE:
After following iltempos tip...I am now getting the following validation error...
ArgumentError: You need to supply at least one validation
from /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
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:in
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'
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: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
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/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'
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 /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:in each'
from /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:in 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/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
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
send'
from /opt/local/lib/ruby/gems/1.8/gems/activerecord-3.2.3/lib/active_record/associations/collection_proxy.rb:46:in `create'