Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/65.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
Ruby on rails 要传递到嵌套资源链接的对象_Ruby On Rails_Ruby On Rails 4 - Fatal编程技术网

Ruby on rails 要传递到嵌套资源链接的对象

Ruby on rails 要传递到嵌套资源链接的对象,ruby-on-rails,ruby-on-rails-4,Ruby On Rails,Ruby On Rails 4,我使用的是嵌套资源,不知道要传递什么对象才能使链接到达正确的位置。我有3个模型帖子,评论和问题,其中评论属于帖子,问题属于评论。我正在尝试从帖子索引页面链接到问题索引页面 这是routes.rb文件的外观: resources :posts do resources :comments do end end resources :comments do resources :questions do end end posts索引文件中的链接: <% post.comm

我使用的是嵌套资源,不知道要传递什么对象才能使链接到达正确的位置。我有3个模型帖子,评论和问题,其中评论属于帖子,问题属于评论。我正在尝试从帖子索引页面链接到问题索引页面

这是routes.rb文件的外观:

resources :posts do
  resources :comments do
 end
end

resources :comments do
  resources :questions do
 end
end
posts索引文件中的链接:

  <% post.comments.select(:body).order('created_at desc').limit(2).each do |comment| %>         

  <%= link_to (comment.body), comment_questions_path(comment, @question) %>
  <% end %>
以下是“rake routes | grep comment_question”的结果:

comment_questions GET    /comments/:comment_id/questions(.:format)   questions#index
comment_question GET    /comments/:comment_id/questions/:id(.:format) questions#show                    

谢谢

由于链接到单个资源,因此正确的路径应该是:

comment_question_path(comment, @question)
如果不起作用,请尝试明确设置注释id:

comment_question_path(comment.id, @question)
此外,如果您可以发布
rake routes | grep comment_question
命令的结果,这将非常有用

编辑: 当
rake routes
输出中的第二个条目显示时,path/url方法需要两个参数

/comments/:comment_id/questions/:id(.:format)
转换为路径方法的符号如下所示(
:id
符号是问题id):

根据错误消息,注释似乎为零,并且可能会返回到URI中的下一个路径(在本例中为
问题

您能否在使用该路径之前抛出调试器语句,并显示
pp comment
pp@question
的输出?


<%= link_to (comment.body), comment_questions_path(comment) %>

Hmm,这两个都给了我一个“找不到id=questions的评论”错误。我将rake routes | grep comment_问题作为问题编辑发布。现在我收到一个“找不到id为comment_id的评论”错误。我无法通过正确使用“pp(评论)”。对我的视图的编辑可能会有帮助(显示循环的内容)。这会给我一个“找不到id=questions的注释”错误。您能检查注释变量的值吗?或者,如果将注释替换为comment.id会发生什么情况?如果您专门查找链接到特定问题的注释,则该路径不正确。假设遵循REST约定,索引操作将使所有问题与注释关联。@MarekTakac comment.id给我相同的“id=questions”错误。我编辑了我的问题,以显示我在视图中循环的内容。您可以添加您的视图吗,因为为了在帖子索引页面上获取对象
注释
,您必须循环浏览例如@posts。看到这些片段可能会有帮助。是的,编辑了这个问题。
comment_question_path(:comment_id, :id)
<%= link_to (comment.body), comment_questions_path(comment) %>