【问题标题】:How to remove controller name in URL when using URL helpers (Rails 4.2.1)使用 URL 帮助程序时如何在 URL 中删除控制器名称 (Rails 4.2.1)
【发布时间】:2015-07-16 12:13:27
【问题描述】:

我以本教程为基础(Railscasts - url 中的模型名称,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 助手?

编辑:耙路线:

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

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

edit3:如果我像教程中那样“手动”创建 slug,一切都会按预期工作。

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

提前致谢!

【问题讨论】:

  • 所有这些路线都将您带到正确的页面,您希望发生什么?抱歉,我不确定我是否关注。
  • rake routes的发布输出
  • @BrianKunzig OP 希望始终将 /page-name 显示为 url 路径..
  • @lac 根据您的路线定义,我得到了正确的路径app.point_path(1).. 给了我"/1"..
  • @BrianKunzig 他们带我去 /points/page-name。如果我在地址行中输入 /page-name,它会将我带到 /page-name。

标签: ruby-on-rails ruby


【解决方案1】:
resources :points, path: "", as: 'direct_points', only: [:show]

这会给你

direct_point_path(point) 

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

【讨论】:

  • 谢谢您,先生,它成功了。没想到同时使用 path: 和 as:
  • 但是这样做会删除所有其他路由。 edit_page_path、new_page_path、page_path……它们都不见了。我现在唯一的路线是:direct_page GET /:id(.:format) pages#show root GET / pages#index
  • @KyleK 所以在它下面添加另一个资源条目,以及你需要的路径。你可以混搭。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-10
  • 1970-01-01
  • 2013-08-15
  • 1970-01-01
  • 2011-08-04
相关资源
最近更新 更多