Ruby on rails 未定义的方法“title';对于#<;ActiveRecord::Relation://post.previous.title
我正在尝试为上一篇文章和下一篇文章创建链接。我之所以不使用Ruby on rails 未定义的方法“title';对于#<;ActiveRecord::Relation://post.previous.title,ruby-on-rails,ruby-on-rails-3,Ruby On Rails,Ruby On Rails 3,我正在尝试为上一篇文章和下一篇文章创建链接。我之所以不使用will_paginate,是因为我不想使用“Previous”文本,而是希望将其作为文章的标题(即post.title) 为此,我遵循另一个答案,在我的帖子模型中创建了以下关系: def previous Post.where(["id < ?", id].last) end def next Post.where(["id < ?", id].first) end 我得到一个错误: un
will_paginate
,是因为我不想使用“Previous”文本,而是希望将其作为文章的标题(即post.title)
为此,我遵循另一个答案,在我的帖子模型中创建了以下关系:
def previous
Post.where(["id < ?", id].last)
end
def next
Post.where(["id < ?", id].first)
end
我得到一个错误:
undefined method `title' for #<ActiveRecord::Relation:0x007f8145616250>
未定义的方法“title”#
我猜这与我在Post模型中定义上一个的方式有关。谢谢你的帮助 您需要修复方法中的括号:)
def-previous
Post.where(“id<?”,id)。最后
结束
def下一个
Post.where(“id>?”,id).first
结束
您需要修复方法中的括号:)
def-previous
Post.where(“id<?”,id)。最后
结束
def下一个
Post.where(“id>?”,id).first
结束
在下一个
方法中,我认为查询应该有一个大于号(>):Post.where(“id>?”,id)。首先
啊,你是对的。你应该对问题本身进行评论,这样OP就会知道:)在下一个方法中,我相信查询应该有一个大于号(>):Post.where(“id>?”,id)。首先你是对的。您应该对问题本身进行评论,以便OP知道:)
undefined method `title' for #<ActiveRecord::Relation:0x007f8145616250>
def previous
Post.where("id < ?", id).last
end
def next
Post.where("id > ?", id).first
end