2011-06-12 46 views
0

我试图在一段时间后重新回到导轨中,并且难以将两个简单的脚手架构建的资源以嵌套的方式连接起来。家长控制员工作,但孩子通常会爆炸。我一直在寻找这个问题的答案,但没有成功。嵌套导轨类和路由的问题

对于一个特定的注释子属于产品父路径 “/产品/ 1 /评论/ 1”

错误消息

找不到没有ID 应用评论/controllers/comments_controller.rb:25:in`节目”

参数:

{ “PRODUCT_ID”=> “1”, “ID”=> “1”}

下面是从comments_controller相关代码 “秀”

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

{“produc T_ID “=>” 1" , “ID”=> “1”}

发表评论指数:/产品/ 1 /评论

错误信息:

未定义的方法`模型名称”的Fixnum对象:类 参数: { “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方式和一个小小的ruby intro书籍,在rails上有几章)基于rails 2,而web资源并没有完全更新。

更新: * 的routes.rb * B方:: Application.routes。绘制做

resources :comments 

resources :products do 
    resources :comments 
end 

从评论索引

未定义的方法`模型名称”的Fixnum对象错误:类

从评论索引相关的代码(在第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> 

另一个更新: * 模型 *

class Product < ActiveRecord::Base 
    has_many :comments 
end 

class Comment < ActiveRecord::Base 
    belongs_to :product 
end 

再次感谢,

卡梅伦

(似乎很奇怪,我认为这不应该工作,因为我已经以下教程。 :/)

+0

你能从router.rb张贴相关的代码吗? – Ant 2011-06-12 22:07:00

+0

更新后的路线信息和错误。感谢您的期待,@Ant&@Harald。 – Sanarothe 2011-06-15 18:20:25

+0

嗯,你的路线和控制器代码对我来说看起来不错,你可以发布你的模型的相关位? – Ant 2011-06-15 22:00:39

回答

0

如果一个注释只能属于一个产品,你应该能够做这样的事在comments_controller.rb:

def show 
    @comment = Comment.find(params[:id]) 
    # @product = @comment.product 
end