2017-05-07 52 views
0

在嵌套的路線評論我在我的應用程序的情況是,comments嵌套在blog裏面是這樣的:刪除的link_to對單獨的頁面

resources :blogs do 
    resources :comments 
end 

我有意見的users#show頁上的列表中用戶需要能夠刪除評論。我users_controller是這樣的:

def show 
    @user = User.find(params[:id]) 
    @comments = Comment.where(approved: false) 
    end 

comments_controller刪除方法是這樣的:

def destroy 
    @blog = Blog.find(params[:blog_id]) 
    @comment = blog.comments.find(params[:id]) 
    @comment.destroy 
    redirect_to blog_path(@blog), notice: 'Comment has been deleted.' 
    end 

最後,這裏是我的users#show頁面上刪除鏈接:

<% @comments.each do |comment| %> 
     <p><strong><%= comment.body %></strong><br /> 
     <em><%= comment.user.first_name %> <%= comment.user.last_name %></em></p> 
     <%= link_to blog_comment_path(@blog, comment), method: :delete, data: { confirm: 'Are you sure?' } do %> 
     <i class="icon ion-trash-a"></i> 
     <% end %> 
     <%= link_to approve_comment_path(comment), method: :post do %> 
     <i class="icon ion-checkmark"></i> 
     <% end %> 
    <% end %> 

我出現以下錯誤:

No route matches {:action=>"show", :blog_id=>nil, :controller=>"comments", :id=>"1"} missing required keys: [:blog_id] 

任何人都可以看到我要去哪裏錯了嗎?

回答

1

你沒有定義@blog還,所以@blognil

您可以編輯,像這樣

<%= link_to blog_comment_path(comment.blog, comment), method: :delete, .... %>

+0

愚蠢的錯誤。優秀。謝謝! – Liz

+0

我很高興聽到這個消息。另一個訣竅:您可以使用'[comment.blog,comment]'而不是'blog_comment_path(comment.blog,comment)'' – fongfan999