簡體   English   中英

Rails'link_to'提供了刪除嵌套資源的錯誤路徑

[英]Rails 'link_to' giving an incorrect path to delete a Nested Resource

我在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>

由chrome的Inspect刪除了我的評論。 那么如何生成正確的鏈接?

控制者

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

您需要使用forum_comment_path幫助器:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM