如何解决嵌套的Rails类和路由存在问题
| 过了一会儿,我试图重回轨道,并且很难以嵌套的方式连接两个简单的脚手架构建资源。父控制器可以工作,但是孩子通常会炸毁。我一直在寻找这个问题的答案,但没有成功。 对于属于产品父级的特定注释子级,请路由\“ / products / 1 / comments / 1 \” 错误信息 没有ID找不到评论 app / controllers / comments_controller.rb:25:在“ show \”中 参数: {\“ product_id \” => \“ 1 \”, \“ id \” => \“ 1 \”} 这是来自comment_controller“ show \”的相关代码def show
@product = Product.find(params[:product_id])
@comment = @product.comments.find(params[:comment_id])
(如果我将:comment_id更改为:id,则新错误是:)
找不到ID = 1的评论[WHERE(comments
.product_id= 1)]
{\“ product_id \” => \“ 1 \”,
\“ id \” => \“ 1 \”}
对于评论索引:/ products / 1 / comments
错误信息:
Fixnum:Class的未定义方法“ model_name \”
参数:
{\“ product_id \” => \“ 1 \”}
**索引视图中的相关代码**
18: <td><%= link_to \'Show\',[@product,comment.id] %></td>
19: <td><%= link_to \'Edit\',edit_product_comment_path(@product,comment) %></td>
20: <td><%= link_to \'Destroy\',comment],:confirm => \'Are you sure?\',:method => :delete %></td>
我已经花了几天的时间搞砸了,但无济于事。我一直在检查诸如:id到:(noun)_id之类的简单内容,以及在我的视图链接中在[@product,comment]和[@product,comment.id]之间切换。
任何建议都非常感谢如何使这项工作。看起来应该很简单,我几乎遵循了“书”。问题是我的rails文本(Rails方式和一些ruby入门书籍以及在rails上有几章的红宝石入门书籍)都是基于rails的充其量2,并且Web资源尚未完全更新。
更新:
* Routes.rb *
Party2 :: Application.routes.draw做
resources :comments
resources :products do
resources :comments
end
评论索引错误
Fixnum:Class的未定义方法“ model_name \”
注释索引中的相关代码(第18行错误)
18: <td><%= link_to \'Show\',:method => :delete %></td>
另一个更新:
*楷模*
class Product < ActiveRecord::Base
has_many :comments
end
class Comment < ActiveRecord::Base
belongs_to :product
end
再次感谢,
卡梅伦
(对我来说似乎很奇怪,这不起作用,因为我一直在关注教程。:/)
解决方法
如果评论只能属于一种产品,则应该可以在comment_controller.rb中执行以下操作:
def show
@comment = Comment.find(params[:id])
# @product = @comment.product
end
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。