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

qichunren · September 10, 2022 · Last by qichunren replied at September 11, 2022 · 426 hits

在 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/'
Reply to shin

这样还是不行的

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

qichunren closed this topic. 11 Sep 00:45
You need to Sign in before reply, if you don't have an account, please Sign up first.