如何允许其他视图在RoR中有delete_class_path?

时间:2010-01-13 13:39:15

标签: ruby-on-rails ruby debugging

我有一个名为“category”的对象,在我的视图/ store / manage.html.erb中,我想这样做:

<%=link_to_remote category.name, :url => delete_category_path(category),
            :confirm => 'Are you sure?', :method => :delete%>

但它向我展示了NoMethodError,我该怎么做? 这是RoR的错误:

undefined method `delete_category_path' for #<ActionView::Base:0x103490da0>

这是我在store_controller.rb中的manage方法:

  def manage

    @categories             = Category.all
    @products               = Product.all

    @category = Category.new(params[:category])
  end

2 个答案:

答案 0 :(得分:1)

您应该只使用category_path(@category)。两个URL都相同,只有HTTP方法更改。在你的情况下,它将是:

<%=link_to_remote category.name, :url => category_path(category),
        :confirm => 'Are you sure?', :method => :delete%>

正如您在rake routes看到的那样:

   categories GET    /categories(.:format)              {:controller=>"categories", :action=>"index"}
              POST   /categories(.:format)              {:controller=>"categories", :action=>"create"}
 new_category GET    /categories/new(.:format)          {:controller=>"categories", :action=>"new"}
edit_category GET    /categories/:id/edit(.:format)     {:controller=>"categories", :action=>"edit"}
     category GET    /categories/:id(.:format)          {:controller=>"categories", :action=>"show"}
              PUT    /categories/:id(.:format)          {:controller=>"categories", :action=>"update"}
              DELETE /categories/:id(.:format)          {:controller=>"categories", :action=>"destroy"}

动作显示,更新和销毁共享相同的category_path

答案 1 :(得分:0)

您必须在config / routes.rb中添加以下代码:

map.resources :categories

(资源 - 为下一个命令创建路径,创建,更新,删除,新建,编辑)。

有关更多信息,请参阅Rails Way书籍或教程rubyonrails.com。