如何在使用URL帮助程序时删除URL中的控制器名称(Rails 4.2.1)

时间:2015-07-16 12:13:28

标签: ruby-on-rails ruby

我使用本教程作为基础(Railscasts - 网址中的模型名称,5:30)https://www.youtube.com/watch?v=963wTp9FCn4

我已添加:

resources :points, path: "", only: [:show]

现在只有在我直接访问该页面时,才能通过/page-name访问该页面。在我的一个观看中使用point_path(point)或仅point时,我需要使用相同的/points/page-name。那么在Rails 4中我还需要修改我的资源丰富的URL帮助器吗?

编辑:rake routes:

Prefix Verb URI Pattern Controller#Action import_points POST /points/import(.:format) points#import points GET /points(.:format) points#index POST /points(.:format) points#create new_point GET /points/new(.:format) points#new edit_point GET /points/:id/edit(.:format) points#edit point GET /points/:id(.:format) points#show PATCH /points/:id(.:format) points#update PUT /points/:id(.:format) points#update DELETE /points/:id(.:format) points#destroy root GET / static_pages#home contact GET /contact(.:format) static_pages#contact portfolio GET /portfolio(.:format) static_pages#portfolio about GET /about(.:format) static_pages#about login GET /login(.:format) sessions#new POST /login(.:format) sessions#create logout GET /logout(.:format) sessions#destroy test GET /test(.:format) static_pages#test GET /:id(.:format) points#show

EDIT2: 我已经在另一个ROR安装上检查了resources :points, path: "", only: [:show]路由并且它可以使用ID。 我正在使用friendly_id gem并且路由匹配:id而不是我的slugged名称。

edit3:如果我创建了slugs"手动"就像在教程中一切都像预期的那样工作。

edit4:如果我将resources :points, path: "", only: [:show]放在路由文件point的顶部,那么帮助程序会给我正确的路径,但我的静态路由会被破坏。

提前致谢!

1 个答案:

答案 0 :(得分:0)

resources :points, path: "", as: 'direct_points', only: [:show]

这会给你

direct_point_path(point) 

您想要的是...它将解析为(application)/(page_name)