2017-05-07 54 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