2015-03-03 75 views
0

我有一個嵌套的資源,像這樣的評論....導軌4:使用「刪除」按鈕問題

resources :microposts do 

    member do 
    get :upvote, :undo_upvote 
    end 

    member do 
    get :follow, :unfollow 
    end 

    resources :responses do 
    member do 
     get :upvote, :undo_upvote 
    end 
    resources :comments 
    end 
end 

我有意見的索引頁面上的刪除按鈕....

<div class = "Comment" id="comment-<%= comment.id %>"> 
    <%= link_to comment_avatar_for(comment.user), comment.user %> 
    <span class="Commentator"> 
     <%= comment.user.full_name %> 
    </span> 
    <span class="content"> 
     <%= comment.body %> 
    </span> 
    <span class="timestamp"> 
    Posted <%= time_ago_in_words(comment.created_at) %> ago. 
    </span> 
    <span class="timestamp"> 
     <% if current_user?(comment.user) %> 
     <%= link_to "delete", comment, method: :delete, data: { confirm: "You sure?" }, :class => "btn btn-default btn-xs delete" %> 
     <% end %> 
    </span> 
</div> 

當網頁加載完畢後我收到此錯誤

undefined method `comment_path' for #<# <Class:0x007f8936876e70>:0x007f8931857020> 

我不清楚爲什麼這是行不通的 - 畢竟我有「C的正確實例omment」。如果有人能指出我朝着正確的方向,我將不勝感激。

回答

1

Rails進行假設。

因爲你的Comment它假定你將使用comment_path,但你沒有那麼按照你的路線,所以你需要設置正確的路徑實例:

<%= link_to "delete", micropost_response_comment_path(@micropost, @response, comment), method: :delete, data: { confirm: "You sure?" }, :class => "btn btn-default btn-xs delete" %> 

我可能有錯誤的路徑,但希望你能明白。

+0

如果想刪除一個'comment',OP應該確定是否需要'@ micropost'和'@ response'資源。 – Magnuss 2015-03-03 13:49:44

+0

也許,我只是按照他給出的代碼去做。 – 2015-03-03 13:50:35

+0

Works ...雖然我對Magnuss的評論感興趣。我假設他的意思,只是具體的路線。 – GhostRider 2015-03-03 13:57:48