Rails删除功能,链接和路由

时间:2014-03-26 11:39:38

标签: ruby-on-rails routes haml

我正在尝试向自定义构建的cms添加删除功能,我在视图中有以下表格:

%table.table.table-hover{ :cellspacing => "0", :cellpadding => "0" }
  %thead
    %th Title
    %th

  - Glossary.find(:all, :order => "title ASC").each do |c|
    %tr{ :onclick => "window.location='/cms/other/glossary/#{ c.id }'" }
      %td= c.title
      %td

在我的控制器中使用它:

def glossary_destroy
  @del = Glossary.find(params[:id])
  @del.destroy

  redirect_to "/cms/other/glossary"
end

但是我不知道在我的路线中放什么,或者如何在我的视图中添加链接以使其工作?

这是我的其他路线:

['glossary', 'gallery'].each do |p|

  match "/cms/other/#{p}" => "cms##{p}"
  match "/cms/other/#{p}/:id" => "cms##{p}_edit"
  match "/cms/other/#{p}-add" => "cms##{p}_add"
  match "/cms/other/#{p}-edit-process" => "cms##{p}_editprocess"
  match "/cms/other/#{p}-add-process" => "cms##{p}_addprocess"

end

1 个答案:

答案 0 :(得分:0)

destroy是REST函数,所以如果你已经添加了

,则不需要在routes.rb中显式添加任何内容。
resources :glossaries

在您的路线中。

在您的视图中(在.each中),您可以使用link_to

来调用它
link_to "destroy", c, method: :delete 

使用您添加的其他路线

在路线文件中添加此项
match "/cms/other/#{p}/:id" => "cms##{p}_destroy", :via => :delete