简体   繁体   English

Ruby on Rails:注释之间的导航

[英]Ruby on Rails: Navigation Between Comments

I'm attempting to create a button on the show page that will allow users to navigate between different comments. 我试图在show页面上创建一个按钮,该按钮将允许用户在不同的注释之间导航。

And for some reason, I keep getting either article :id or comment :id could not be found. 由于某种原因,我一直找不到article :idcomment :id

Any idea why that happens? 知道为什么会这样吗?

#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)

EDIT: The problem was in my routes, which has now been fixed. 编辑:问题出在我的路线上,现已修复。 But now I get the following error: 但是现在我得到以下错误:

undefined method "next" for nil:NilClass

EDIT 2: 编辑2:

Here's the log--and the URL goes to http://localhost:3000/articles/14/comments/56/next_comment 这是日志-URL转到http://localhost:3000/articles/14/comments/56/next_comment

Now it doesn't raise any errors but simply displays a blank page instead of going to the next comment URL which is http://localhost:3000/articles/14/comments/70 现在它不会引发任何错误,而只是显示空白页,而不是转到下一个注释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)

The @scope instance variable is undefined (and therefore nil ) prior to you using it. 在使用@scope实例变量之前,它是未定义的(因此为nil )。 Just change it to the following and it should work 只需将其更改为以下内容即可

def next_comment
  @next_comment = scope.next(@comment)
end

def scope
  @scope ||= @article.comments
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM