开发者

How do I specify an action on a resource in a namespace in rails 3?

I have a resource :products in a namespace :shop, like this:

namespace :shop do
  resources :products
  root :to => 'products#index'
end

When running rake routes it outputs the following:

edit_shop_product GET    /shop/products/:id/edit(.:format) {:action=>"edit", :controller=>"shop/products"}

But when I use the edit_shop_product_path in a partial view, like this:

<%= button_to "Edit", edit_shop_product_path(product) %>

I get an ActionController Exception: No route matches "/shop/products/1/edit"

Wha开发者_StackOverflow社区t am I missing?


Ok, I found the answer myself in the end. The problem is the button_to method which defaults to generating a POST http request. By changing this to a GET like this:

<%= button_to "Edit", edit_shop_product_path(product), :method => :get %>

Or by using the link_to method it works as advertised.


  1. Create a directory named shop under the controllers/ directory
  2. Create a ruby file named products_controller.rb under the shop directory
  3. Name the controller class Shop::ProductsController < BaseController
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜