Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Loops 轨道在循环中循环_Loops_Ruby On Rails 4_Each - Fatal编程技术网

Loops 轨道在循环中循环

Loops 轨道在循环中循环,loops,ruby-on-rails-4,each,Loops,Ruby On Rails 4,Each,我有文章模型,并在索引页中循环显示文章。我有属于Post的评论,所以在show Post页面中我使用了loop。但是帖子有一个响应,所以当我在点击addresponse后写“loop Response in the loop comment”时,我会得到一个警告 响应已成功添加 但在评论中没有显示,我认为这是一个断环: post#show <% @comments.each do |comment| %> <p> Author: <%= comment.use

我有文章模型,并在索引页中循环显示文章。我有属于Post的评论,所以在show Post页面中我使用了loop
。但是帖子有一个响应,所以当我在点击
addresponse
后写“loop Response in the loop comment”时,我会得到一个警告

响应已成功添加

但在评论中没有显示,我认为这是一个断环:

post#show

<% @comments.each do |comment| %>

  <p> Author: <%= comment.user.nickname %></p>
  <p><%= comment.body %></p>

  <%= link_to "Add a response to comment!",
        new_post_comment_response_path(@post, comment) %>

  <% @responses.each do |response| %>
    <%= response.body_response %>
  <% end %>

<% end %>

作者:


错误吗?

您不能执行
@responses.each do | response |
,因为您尚未在控制器中定义
@responses
实例变量。。。您不能这样做,因为每个
回复
集合对于每个
评论

假设你有一个
在你的评论类中有很多:回复
,你可以做

  <% comment.responses.each do |response| %>

您不能执行
@responses.each do | response |
,因为您尚未在控制器中定义
@responses
实例变量。。。您不能这样做,因为每个
回复
集合对于每个
评论

假设你有一个
在你的评论类中有很多:回复
,你可以做

  <% comment.responses.each do |response| %>

我认为你想要的方式是,你会有一篇帖子,可以有很多评论,并且会有回复

这是您的模型中应该包含的内容:

Post.rb --> :has_many comments
Comment.rb --> :belongs_to post

Comment.rb --> :has_many responses
Response.rb --> :belongs_to comment
在此之后,您可以执行以下操作:

显示特定帖子的所有评论和回复
我认为你想要的方式是,你将有一个帖子,可以有很多评论,并会有回应的评论

这是您的模型中应该包含的内容:

Post.rb --> :has_many comments
Comment.rb --> :belongs_to post

Comment.rb --> :has_many responses
Response.rb --> :belongs_to comment
在此之后,您可以执行以下操作:

显示特定帖子的所有评论和回复
请提供config/routes.rbWhat is
@responses
?假设变量按预期分配,代码看起来是正确的。请提供config/routes.rbWhat is
@responses
?假设变量按预期分配,代码看起来是正确的。所以我就这么做了,并且:在注释下,我有所有的响应,而不仅仅是属于注释id的响应。我的@responses在def show end方法中是:
@responses=Response.where(:Comment\u id=>params[:Comment\u id])。order(“created\u at DESC”)
我想得到属于注释下id的响应。如何得到它?谢谢大家的回复!我很高兴!“comment属于Post,有很多:responses”,所以你应该有一个方法“comment.responses”,你应该使用它。如果您执行
@responses=Response.where(post\u id:@post.id).
您将获得该帖子的所有回复。正如你所说,你看到了。你试过我答案中的代码了吗?当我使用@responses=Response.find(params[:review_id])时,我有一个错误:“找不到id=Response的响应”你为什么这么做?这不是我的回答所暗示的。我的回答是将视图文件更改为使用
您不需要
@responses
实例变量。抱歉,我不理解您的意思,因此如果我不在Post controller中设置@responses,而只更改视图文件,则响应不会仍然显示。我的观点:它不起作用,所以我只是这样做,并且:在注释下,我有所有的响应,而不仅仅是属于注释id的响应。在def show end方法中,我的@responses是:
@responses=Response。其中(:Comment\u id=>params[:Comment\u id])。order(“created\u at DESC”)
我想在注释下得到属于id的响应。如何得到它?谢谢大家的回复!我很高兴!“comment属于Post,有很多:responses”,所以你应该有一个方法“comment.responses”,你应该使用它。如果您执行
@responses=Response.where(post\u id:@post.id).
您将获得该帖子的所有回复。正如你所说,你看到了。你试过我答案中的代码了吗?当我使用@responses=Response.find(params[:review_id])时,我有一个错误:“找不到id=Response的响应”你为什么这么做?这不是我的回答所暗示的。我的回答是将视图文件更改为使用
您不需要
@responses
实例变量。抱歉,我不理解您的意思,因此如果我不在Post controller中设置@responses,而只更改视图文件,则响应不会仍然显示。我的观点是:它不起作用