Rails Rails 路由怎么匹配生成一个带有 "/"结尾的 的 URL?

qichunren · 2022年09月10日 · 最后由 qichunren 回复于 2022年09月11日 · 432 次阅读

在 Rails routes.rb 中

  • get "/:name", to: "hello#welcome", as: :hello
  • get "/:name/", to: "hello#welcome", as: :hello2

两种形式都是生成同一个形式的 URL, hello_path(name: "Jim") 生成为 /Jim, hello2_path(name: "Jim") 也是生成为 /Jim,而不是 /Jim/,

有什么办法解决这个问题呢?

get '/name', to: 'hello#welcome', as: '/hello/'
shin 回复

这样还是不行的

get "/:name", to: "hello#welcome", trailing_slash: true, as: :hello2 ,加了这个后,hello2_path(name: "Jim") 生成了 /Jim/ 这样的 URL,但是 /Jim 这个 URL 还是能匹配到,只能这样了。

qichunren 关闭了讨论。 09月11日 00:45
需要 登录 后方可回复, 如果你还没有账号请 注册新账号