Rails' link_to'提供不正确的路径来删除嵌套资源

时间:2016-01-05 19:55:51

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

我在rails中创建了一个简单的博客应用程序,当我尝试提供删除注释的链接时,rails给了我错误的链接

<a data-method="delete" href="/forums/27/comment.7" rel="nofollow">Delete</a>

这给了我一个错误

No route matches [DELETE] "/forums/27/comment.7"

但是当我把它改为

<a data-method="delete" href="/forums/27/**comments/7**" rel="nofollow">Delete</a>

by chrome&#39>检查它删除了我的评论。 那么如何生成正确的链接?

控制器

def destroy
  @comment.destroy
  redirect_to :back
end

private

  def find_forum 
    @forum = Forum.find(params[:forum_id])
  end

  def set_comment
    @comment = Comment.find(params[:id])
  end

show.html.haml

- @forum.comments.each do |c|
  = c.user.firstname
  = c.comment
  - if user_signed_in?
    - if c.user == current_user
      = link_to "Delete", [@forum, c], method: :delete

路线

                  Prefix Verb   URI Pattern                                   Controller#Action
              forum_like PUT    /forums/:forum_id/like(.:format)              forums#like
            forum_unlike PUT    /forums/:forum_id/unlike(.:format)            forums#unlike
           forum_comment PUT    /forums/:forum_id/comment(.:format)           forums#comment
          forum_comments GET    /forums/:forum_id/comments(.:format)          comments#index
                         POST   /forums/:forum_id/comments(.:format)          comments#create
       new_forum_comment GET    /forums/:forum_id/comments/new(.:format)      comments#new
      edit_forum_comment GET    /forums/:forum_id/comments/:id/edit(.:format) comments#edit
                         GET    /forums/:forum_id/comments/:id(.:format)      comments#show
                         PATCH  /forums/:forum_id/comments/:id(.:format)      comments#update
                         PUT    /forums/:forum_id/comments/:id(.:format)      comments#update
                         DELETE /forums/:forum_id/comments/:id(.:format)      comments#destroy
                  forums GET    /forums(.:format)                             forums#index
                         POST   /forums(.:format)                             forums#create
               new_forum GET    /forums/new(.:format)                         forums#new
              edit_forum GET    /forums/:id/edit(.:format)                    forums#edit
                   forum GET    /forums/:id(.:format)                         forums#show
                         PATCH  /forums/:id(.:format)                         forums#update
                         PUT    /forums/:id(.:format)                         forums#update
                         DELETE /forums/:id(.:format)                         forums#destroy
        new_user_session GET    /users/sign_in(.:format)                      devise/sessions#new
            user_session POST   /users/sign_in(.:format)                      devise/sessions#create
    destroy_user_session DELETE /users/sign_out(.:format)                     devise/sessions#destroy
           user_password POST   /users/password(.:format)                     devise/passwords#create
       new_user_password GET    /users/password/new(.:format)                 devise/passwords#new
      edit_user_password GET    /users/password/edit(.:format)                devise/passwords#edit
                         PATCH  /users/password(.:format)                     devise/passwords#update
                         PUT    /users/password(.:format)                     devise/passwords#update
cancel_user_registration GET    /users/cancel(.:format)                       registrations#cancel
       user_registration POST   /users(.:format)                              registrations#create
   new_user_registration GET    /users/sign_up(.:format)                      registrations#new
  edit_user_registration GET    /users/edit(.:format)                         registrations#edit
                         PATCH  /users(.:format)                              registrations#update
                         PUT    /users(.:format)                              registrations#update
                         DELETE /users(.:format)                              registrations#destroy
                    root GET    /                                             forums#index

1 个答案:

答案 0 :(得分:1)

您需要使用forum_comment_path帮助程序:

= link_to "Delete", forum_comment_path(@forum, c), method: :delete