2016-09-22 95 views
0

UPDATE學習導軌 - 上刪除

我已經延長我的應用程序有一個名爲「標記」(非常類似的設置,如在博客評論)新模式後,改寫路由錯誤。

預期結果:用戶在點擊link_to按鈕刪除標籤時停留在頁面上;我現在得到一個路由錯誤(下面),我不明白爲什麼。

沒有路由匹配[刪除] 「/註釋/ 7 /標籤」

標籤列表中添加到視圖的註解是這樣的:

<div class="panel panel-default" style="background-color: white; word-wrap: break-word; font-size: 0.9em;"> 
    <table id="tags" class="table table-hover"> 
     <thead> 
      <tr> 
       <th>Tag</th> 
       <th>Type</th> 
       <th></th> 
      </tr> 
     </thead> 
     <tbody> 
      <% @annotation.tags.each do |tag| %> 
       <tr> 
        <td><%= tag.content %></td> 
        <td><%#= tag.tagtype_id | tag.tagtype.typeoftag %></td> 
        <td><%= link_to '', [tag.annotation, tag], method: :delete, data: { confirm: 'Please confirm deletion!' }, :class => "glyphicon glyphicon-remove" %></td> 
       </tr> 
      <% end -%> 
     </tbody> 
    </table> 
</div> 

這些都是我的路線:

Rails.application.routes.draw do 
    root 'dashboard#index' 
    devise_for :users 
    resources :users, :documenttypes, :tagtypes 

    resources :documents do 
    resources :tags 
    get "pdf", on: :member 

    end 

    resources :annotations do 
    resources :comments, :tags 
    get "pdf", on: :member 

end 

get "annotations/:id/annotate" => "annotations#annotate", as: 'annotate' 
get "angular_test", to: "angular_test#index" 

mount PdfjsViewer::Rails::Engine => "/pdfjs", as: 'pdfs' 

而且tags.controller

class TagsController < ApplicationController 

def create 
@annotation = Annotation.find(params[:annotation_id]) 
@comment = @annotation.tags.create(tag_params) 
redirect_to annotation_path(@annotation) 
end 

def destroy 
    @annotation = Annotation.find(params[:annotation_id]) 
    @comment = @annotation.tags.find(params[:id]) 
    @comment.destroy 
    redirect_to annotate_path(@annotation) 
end 

private 
def tag_params 
    params.require(:tag).permit(:content, :location, :tagtype_id) 
end 

end 

UPDATE 表視圖始終有一個空行,這是我無法刪除。然後我得到路由錯誤。使用添加標籤時創建的行,這不會發生,我可以刪除它們。爲什麼我得到一個空行?

+1

是的,你可以在沒有Jquery的情況下使用普通的javascript。 –

+0

沒有普通的JS,只使用rails/ruby​​/erb? –

+1

是的,頁面重新加載。 –

回答

0

這需要使用AJAX(異步)完成,所有你需要做的就是讓你的銷燬行動來響應JavaScript。你可以通過添加遠程選項的鏈接如下缺失:

<td><%= link_to 'Delete', [tag.annotation, tag], method: :delete, remote: true, data: { confirm: 'Please confirm deletion!' }, :class => "glyphicon glyphicon-remove" %></td> 

remote: true選項發出請求AJAX。現在在控制器端,您需要用您想要執行的結果JavaScript代碼進行響應,可能隱藏了您要刪除的這一行。

在views文件夾下的tags控制器中,您需要創建一個名爲destroy.js的文件,並在該文件中指定要執行的代碼,以便處理該代碼以隱藏該行。

+0

讓它在沒有AJAX的情況下工作(在標籤控制器上需要正確的redirect_to)。剩下的是總是出現的空行(不存在標籤)(見底部更新),並且我也不能刪除(路由錯誤)。 –