【发布时间】: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