Rails帮助无法删除对象

时间:2011-03-05 18:23:10

标签: ruby-on-rails ruby-on-rails-3

我遇到了问题。我无法删除kategoris。

耙路线:

       admin_kategoris GET    /admin/kategoris(.:format)               {:action=
>"index", :controller=>"admin/kategoris"}
                        POST   /admin/kategoris(.:format)              {:action=
>"create", :controller=>"admin/kategoris"}
     new_admin_kategori GET    /admin/kategoris/new(.:format)          {:action=
>"new", :controller=>"admin/kategoris"}
    edit_admin_kategori GET    /admin/kategoris/:id/edit(.:format)     {:action=
>"edit", :controller=>"admin/kategoris"}
         admin_kategori GET    /admin/kategoris/:id(.:format)          {:action=
>"show", :controller=>"admin/kategoris"}
                        PUT    /admin/kategoris/:id(.:format)          {:action=
>"update", :controller=>"admin/kategoris"}
                        DELETE /admin/kategoris/:id(.:format)          {:action=
>"destroy", :controller=>"admin/kategoris"}

我的kategori索引视图:

<% @kategoris.each do |kategori| %>
  <tr>
    <td><%= kategori.name %></td>
    <td><%= link_to 'show', {:action => 'show', :id => kategori.id}, :class => 'action show' %></td>
    <td><%= link_to 'edit', {:action => 'edit', :id => kategori.id}, :class => 'action edit' %></td>
    <td><td><%= link_to 'slet', {:action => 'destroy', :id => kategori.id, :method => :delete}, :class => 'action destroy' %></td></td>
  </tr>
<% end %>

我的kategori控制器:

 def destroy
    @kategori = Kategori.find(params[:id])
    @kategori.destroy

    respond_to do |format|
      format.html { redirect_to(kategoris_url) }
      format.xml  { head :ok }
    end
  end

我的routes.rb:

namespace :admin do
resources :kategoris
end

按下slet(删除)时出现以下错误:

Started GET "/admin/kategoris/1?method=destroy" for 127.0.0.1 at 2011-03-05 20:0
8:57 +0100
  Processing by Admin::KategorisController#show as HTML
  Parameters: {"method"=>"destroy", "id"=>"1"}
  ←[1m←[36mKategori Load (1.0ms)←[0m  ←[1mSELECT `kategoris`.* FROM `kategoris`
WHERE (`kategoris`.`cached_slug` = '1') LIMIT 1←[0m
  ←[1m←[35mSQL (1.0ms)←[0m  SELECT sluggable_id FROM slugs WHERE ((slugs.sluggab
le_type = 'Kategori' AND slugs.name = '1' AND slugs.sequence = 1))
  ←[1m←[36mKategori Load (0.0ms)←[0m  ←[1mSELECT `kategoris`.* FROM `kategoris`
WHERE (`kategoris`.`id` = 1) LIMIT 1←[0m
Rendered admin/kategoris/show.html.erb within layouts/application (5.0ms)
Completed   in 166ms

ActionView::Template::Error (No route matches {:action=>"edit", :controller=>"ka
tegoris", :id=>#<Kategori id: 1, name: "Elektronik", created_at: "2011-02-17 04:
18:11", updated_at: "2011-02-17 04:18:11", cached_slug: "">}):
    6: </p>
    7:
    8:
    9: <%= link_to 'Edit', edit_kategori_path(@kategori) %> |
    10: <%= link_to 'Back', admin_kategoris_path %>
  app/views/admin/kategoris/show.html.erb:9:in `_app_views_admin_kategoris_show_
html_erb__679400070_47327076__118033797'
  app/controllers/admin/kategoris_controller.rb:18:in `show'

Rendered C:/Ruby192/lib/ruby/gems/1.9.1/gems/actionpack-3.0.3/lib/action_dispatc
h/middleware/templates/rescues/_trace.erb (1.0ms)
Rendered C:/Ruby192/lib/ruby/gems/1.9.1/gems/actionpack-3.0.3/lib/action_dispatc
h/middleware/templates/rescues/_request_and_response.erb (4.0ms)
Rendered C:/Ruby192/lib/ruby/gems/1.9.1/gems/actionpack-3.0.3/lib/action_dispatc
h/middleware/templates/rescues/template_error.erb within rescues/layout (57.0ms)

我的编辑链接有效。

2 个答案:

答案 0 :(得分:3)

使用Rails的RESTful操作时,destroy方法由DELETE请求调用(由rake routes输出开头的“DELETE”指示),而不是GET请求(这是默认值,除非您另行指定)。因此,您需要在链接中指定:

<td><%= link_to 'slet', {:action => 'destroy', :id => kategori.id}, :method => :delete, :class => 'action destroy' %></td>

这可以简化为:

<td><%= link_to 'slet', admin_kategori_path(kategori), :method => :delete, :class => 'action destroy' %></td>

因为admin_kategori是指向正确URL的命名路由,并且使用DELETE方法将自动调用destroy操作(因为这就是路由所说的)。

[更新]

您也可能没有正确设置UJS。你需要:

  1. 确保rails.js脚本标记(以及它可能依赖的任何库,如Prototype或jQuery,具体取决于您的设置)包含在您的布局中,并且
  2. 确保您在布局的头部输出csrf_meta_tag。如果你不是,它将无法正常工作

答案 1 :(得分:0)

看看这里:http://guides.rubyonrails.org/routing.html#crud-verbs-and-actions

要以REST样式格式销毁资产,您需要将您的http请求设为DELETE

因此,您需要添加其他选项::method => :delete