views:

14

answers:

2

I have a object called "category", in my view/store/manage.html.erb, I want to do this :

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

But it show me the NoMethodError, how can I do that? This is the error from RoR:

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

This is my manage method in the store_controller.rb:

  def manage

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

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

You have to add to the config/routes.rb the next following code:

map.resources :categories

(Resources - create paths for the next commands, create, update, delete, new, edit).

For more information read in the Rails Way book or in the tutorials rubyonrails.com.

oivoodoo
I already have the " map.resources :categories " in my routes.rb....why I still get the errors?
Ted Wong
I appologize, I missed that you wann to use delete method.In link_to_remote ... :url => category_path(category), :method => :delete.
oivoodoo
+1  A: 

You should use just category_path(@category). Both URL's are the same and only the HTTP method changes. In your case it would be:

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

As you can see with 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"}

The actions show, update and destroy share the same category_path.

Tomas Markauskas