我是rails新手,我有一个映射到resources的名为resource_links的控制器
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吗
发布于 2012-04-10 13:19:26
只需尝试:
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-10 13:24:52
可以,您可以通过这种方式扩展默认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#destroyhttps://stackoverflow.com/questions/10083325
复制相似问题