[英]Ruby on Rails: Navigation Between Comments
我试图在show
页面上创建一个按钮,该按钮将允许用户在不同的注释之间导航。
由于某种原因,我一直找不到article :id
或comment :id
。
知道为什么会这样吗?
#comment.rb
def self.next(comment, key = :id)
self.where("#{key} > ?", comment.send(key)).first
end
#comments_controller.rb
class CommentsController < ApplicationController
before_action :find_article
before_action :find_comment
def next_comment
@next_comment = @scope.next(@comment)
end
def scope
@scope = @article.comments
end
end
#comments/show.html.haml
= link_to "Next Comment", comments_next_comment_path(@next_comment)
编辑:问题出在我的路线上,现已修复。 但是现在我得到以下错误:
undefined method "next" for nil:NilClass
编辑2:
这是日志-URL转到http://localhost:3000/articles/14/comments/56/next_comment
现在它不会引发任何错误,而只是显示空白页,而不是转到下一个注释URL,即http://localhost:3000/articles/14/comments/70
Parameters: {"article_id"=>"14", "id"=>"56"}
Article Load (0.2ms) SELECT "articles".* FROM "articles" WHERE "articles"."id" = ? LIMIT 1 [["id", 14]]
Comment Load (0.1ms) SELECT "comments".* FROM "comments" WHERE "comments"."id" = ? LIMIT 1 [["id", 56]]
Rendered comments/next_comments.html.haml within layouts/application (0.2ms)
在使用@scope
实例变量之前,它是未定义的(因此为nil
)。 只需将其更改为以下内容即可
def next_comment
@next_comment = scope.next(@comment)
end
def scope
@scope ||= @article.comments
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.