RAILS 4:'删除'按键

时间:2015-03-03 13:40:15

标签: ruby-on-rails nested nested-resources

我有一个嵌套资源,像这样的评论......

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>

我不确定为什么这不起作用 - 毕竟我有'评论'的正确实例。如果有人能指出我正确的方向,我将不胜感激。

1 个答案:

答案 0 :(得分:1)

Rails做出假设。

因为你有一个Comment的实例,它会假设你将使用comment_path,但是根据你的路线没有这个,所以你需要设置正确的路径: / p>

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

我可能错路,但希望你明白了。