Ruby on rails 语法错误意外的tANDOP on rails 3教程
我想我忽略了一些愚蠢的事情,但我遵循的是rails 3教程,如果用户对未发布的文章发表评论,我会添加错误……因此在rails控制台中,我尝试通过输入以下代码来创建对“未发布”文章(草稿)的评论Ruby on rails 语法错误意外的tANDOP on rails 3教程,ruby-on-rails,ruby-on-rails-3,syntax-error,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.c
>> 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!'
以下是我的文章模型:
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
类文章true
验证:body,:presence=>true
属于:用户
_和_是否属于多个类别
有很多评论
范围:已发布,其中(“articles.published_at不为空”)
范围:草稿,其中(“articles.published_at为空”)
#最近发表的文章<一个月前-参见教程第103页
#范围:最近,lambda{published.where(“articles.published_at>?”,1.month.ago.to_date)}
范围:where_title,lambda{term}where(“articles.title LIKE?”,“%{term}%”
def long_标题
“#{title}-#{published_at}”
结束
def发布了吗?
现在出版了吗?
结束
结束
请让我知道,如果我需要提供任何其他…我感谢任何和所有的帮助…非常感谢
更新:
在遵循iltempos提示之后…我现在得到以下验证错误
ArgumentError:您至少需要提供一个验证
来自/opt/local/lib/ruby/gems/1.8/gems/activemodel-3.2.3/lib/active\u model/validations/validates.rb:86:invalidates'
from/Users/bbarton250/Sites/rails_projects/theoldman/app/models/comment.rb:7
从/opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active\u support/dependencies.rb:469:in
load'
从/opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active\u support/dependencies.rb:469:inload\u文件'
从/opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active\u support/dependencies.rb:639:in
new\u constants\u in'
来自/opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active\u support/dependencies.rb:468:inload\u文件'
从/opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active\u support/dependencies.rb:353:in
require\u或\u load'
从/opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active\u support/dependencies.rb:502:inload\u missing\u constant'
从/opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active\u-support/dependencies.rb:192:in
const\u-missing'
来自/opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active\u support/dependencies.rb:190:ineach'
从/opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active\u support/dependencies.rb:190:in
const\u missing'
从/opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active\u support/dependencies.rb:514:inload\u missing\u constant'
从/opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active\u-support/dependencies.rb:192:in
const\u-missing'
来自/opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active\u support/dependencies.rb:190:ineach'
从/opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active\u support/dependencies.rb:190:in
const\u missing'
来自/opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active\u-support/endictor/methods.rb:218:inconstantize'
来自/opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active\u-support/endictor/methods.rb:217:in
each'
来自/opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active\u-support/endoctor/methods.rb:217:inconstantize'
从/opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active\u support/dependencies.rb:554:in
get'
来自/opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active\u support/dependencies.rb:588:inconstantize'
从/opt/local/lib/ruby/gems/1.8/gems/activerecord-3.2.3/lib/active\u record/heritation.rb:111:in
compute\u type'
来自/opt/local/lib/ruby/gems/1.8/gems/activerecord-3.2.3/lib/active\u record/heritation.rb:109:in每个'
从/opt/local/lib/ruby/gems/1.8/gems/activerecord-3.2.3/lib/active\u record/heritation.rb:109:in
compute\u type'
从/opt/local/lib/ruby/gems/1.8/gems/activerecord-3.2.3/lib/active\u record/reflection.rb:172:insend'
从/opt/local/lib/ruby/gems/1.8/gems/activerecord-3.2.3/lib/active\u record/reflection.rb:172:in
klass'
来自/opt/local/lib/ruby/gems/1.8/gems/activerecord-3.2.3/lib/active\u record/associations/collection\u association.rb:148:intransaction'
从/opt/local/lib/ruby/gems/1.8/gems/activerecord-3.2.3/lib/active\u record/associations/collection\u association.rb:431:in
create\u record'
来自/opt/local/lib/ruby/gems/1.8/gems/activerecord-3.2.3/lib/active\u record/associations/collection\u association.rb:119:increate'
从/opt/local/lib/ruby/gems/1.8/gems/activerecord-3.2.3/lib/active\u record/associations/collection\u proxy.rb:46:in
发送'
从/opt/local/lib/ruby/gems/1.8/gems/activerecord-3.2.3/lib/active\u record/associations/collection\u proxy.rb:46:in“create”看起来有一个不应该出现的换行符:
def article_should_be_published
errors.add(:article_id, "is not published yet") if article && !article.published?
end
谢谢iltempo…不过我现在得到了新的错误。我已经把它包括在上面了。关于这个“需要提供至少一个验证”错误有什么想法吗?不管怎样,我使用了“验证:”当我应该使用“验证:”…thx来获得帮助时
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
def article_should_be_published
errors.add(:article_id, "is not published yet") if article && !article.published?
end