2011-06-12 64 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