我正试图在一段时间后回到rails中,并且很难以嵌套的方式连接两个简单的脚手架构建的资源。父控制器工作,但孩子通常会爆炸。我一直在寻找这个问题的答案,但没有成功。
对于属于产品父级的特定注释子节点,请路由“/products/1/注释/1”
错误消息
如果没有ID应用程序/控制器/注释_控制器.in:25:in‘`show’,
无法找到注释
参数:
{"product_id"=>"1",“id”“=>”1“}
这里是comments_controller "show"的相关代码
def show
@product = Product.find(params[:product_id])
@comment = @product.comments.find(params[:comment_id])(如果我将:comment_id更改为just :id,新的错误是:)
找不到ID=1的评论(
comments.product_id = 1)
{"product_id"=>"1",“id”“=>”1“}
注释索引的:/products/1/comments
错误信息:
用于Fixnum的
未定义方法`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', [@product, comment], :confirm => 'Are you sure?', :method => :delete %></td>我花了几天时间搞砸这件事,但没有用。我一直在检查简单的东西,比如:id到:(名词)_id,以及在我的观点中在@product、comment和@product、comment.id之间切换链接。
对于如何使这项工作有效,任何建议都会受到极大的赞赏。这看起来应该很简单,我很喜欢“书”。这方面的问题是,我的rails文本( Rails方法和一本带有几章rails的小红宝石入门书)充其量都是基于rails 2的,而且web资源还没有完全更新。
更新: *Routes.rb* Party2::Application.routes.draw do
resources :comments
resources :products do
resources :comments
end注释索引中的错误
用于Fixnum:Class的
未定义方法`model_name‘
注释索引中的相关代码(第18行出错)
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', [@product, comment], :confirm => 'Are you sure?', :method => :delete %></td>另一个更新: *Models*
class Product < ActiveRecord::Base
has_many :comments
end
class Comment < ActiveRecord::Base
belongs_to :product
end再次感谢,
卡梅伦
(对我来说,这不应该是很奇怪的,因为我一直在学习教程。*/)
发布于 2011-06-12 22:35:46
如果一个注释只能属于一个产品,那么您应该能够在comments_controller.rb中执行这样的操作:
def show
@comment = Comment.find(params[:id])
# @product = @comment.product
endhttps://stackoverflow.com/questions/6324953
复制相似问题