0

我是一个 Rails 新手,我有一个控制器resource_links,我已经映射到resources

resources :resources, :as => :resource_links, :controller => :resource_links

这有效(基本上/resources作为/resource_links)。然而,试图去/resources/tags是行不通的。为了解决这个问题,我添加了更多映射:

  match 'resource_links/tag/:tag(.:format)' => 'resource_links#tag', :via => :get, :as => 'resource_links_tagged', :constraints => {:tag => /.*/}
  match 'resource_links/tags' => 'resource_links#tags', :via => :get, :as => 'resource_links_tags'

有什么办法可以/resources/tags映射到/resource_links/tag吗?

4

2 回答 2

0

只需尝试:

  match 'resource/tag/:tag(.:format)' => 'resource_links#tag', :via => :get, :as => 'resource_links_tagged'
  match 'resource/tags' => 'resource_links#tags', :via => :get, :as => 'resource_links_tags'

或使用 LIKE(更具可读性):

  match 'resource/tag/:tag(.:format)', :controller => 'resource_links', :action => 'tag', :via => :get, :as => 'resource_links_tagged'
  match 'resource/tags', :controller => 'resource_links', :action => 'tags', :via => :get, :as => 'resource_links_tags'
于 2012-04-10T05:19:25.847 回答
0

是的,您可以通过这种方式扩展默认的 restful 路由

resources :resources, :as => :resource_links, :controller => :resource_links do
  collection do
    get :tags
    get 'tag/:tag', :action => :tag, :as => :tagged
  end
end

接下来,查看输出rake routes以检查路由助手的命名方式。

  tags_resource_links GET    /resources/tags(.:format)     resource_links#tags
tagged_resource_links GET    /resources/tag/:tag(.:format) resource_links#tag
       resource_links GET    /resources(.:format)          resource_links#index
                      POST   /resources(.:format)          resource_links#create
    new_resource_link GET    /resources/new(.:format)      resource_links#new
   edit_resource_link GET    /resources/:id/edit(.:format) resource_links#edit
        resource_link GET    /resources/:id(.:format)      resource_links#show
                      PUT    /resources/:id(.:format)      resource_links#update
                      DELETE /resources/:id(.:format)      resource_links#destroy
于 2012-04-10T05:24:52.070 回答