1

目前我有这样的事情:

  resources :books do
    collection do
      get 'search'
    end
  end

我的控制器名称也是“books”,里面有一个名为“search”的操作方法

我希望“获取搜索”部分也成为一种资源,有点像嵌套资源......但我不想破坏其他人的代码,这些代码正在使用它生成的当前路线,所以需要更新它被动的方式!

4

2 回答 2

1
resources :books do
  collection do
    get 'search'
  end
  resources :searches
end

...如果我对您的理解正确,那应该就是您想要的。它不会破坏其他路线,只需添加新路线。


运行rake routes以确保您拥有所需/需要的所有路线。

于 2013-03-07T16:19:59.730 回答
0

使用嵌套的浅层路由,例如:

resources :books , :shallow => true do
  resources :searches
end

现在您将获得以下路线:

/books/1 => books_path(1)
/books/1/searches => books_searches_index_path(1)
/searches/2 => searches_path(2)

同样,您可以为已定义的路由获取单独的路由,例如:

get '(:books)/searches', :to => 'books#index'
于 2013-03-07T16:43:31.080 回答