2016-09-22 85 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)。剩下的是总是出现的空行(不存在标签)(见底部更新),并且我也不能删除(路由错误)。 –