Rails中的用户名路由(Username Routing in Rails)

编程入门 行业动态 更新时间:2024-10-26 02:33:29
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.

更多推荐

本文发布于:2023-07-24 20:12:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1250595.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:路由   用户名   Rails   Username   Routing

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!