Rails中的用户名路由(Username Routing in Rails)
我正在尝试创建一个项目,其中/ username被重定向到该用户名的配置文件。 我怎样才能做到这一点?
I am trying make a project in which /username gets redirected to that username's profile. How can I make this happen?
最满意答案
路线为: "get /:username", to: "users#profile"
您可以将users#profile更改为任何您的控制器操作调用。
你需要确保把它放在你的路线的尽头 。 否则,它会拦截你的所有路线 。
例如,不要执行以下操作:
get "/:username", to: "users#profile" get "/foo", to: "pages#bar"因为你永远无法访问pages#bar端点。
The route would be: "get /:username", to: "users#profile"
You would change users#profile to whatever your controller action is called.
You need to make sure to put this at the end of your routes. Otherwise it will intercept all your routes.
For example, don't do the following:
get "/:username", to: "users#profile" get "/foo", to: "pages#bar"Because you will never be able to reach the pages#bar endpoint.
更多推荐
发布评论