如何在 Rails 3 中为每个用户提供不同的视图?

发布于 2024-11-19 20:08:13 字数 679 浏览 4 评论 0原文

所以我有我的表users,每条记录都有一个username字段

,我有控制器main的根和操作`index,

root :to => 'pages#main'

所以如果转到http://mydomain.com 它将显示我的 main#index 页面,我有 aboutcontact 页面也是如此。

但如果我访问http://mydomain.com/Mr_Nizzle,它会向我显示位于users#show上的用户Mr_Nizzle的页面(就像示例一样)以及其他用户显示每个用户的页面...

如果我去 => 是正确的吗?

match ':username' => 'users#show'

match 'contact_us' => 'main#contact'

match 'about' => 'main#about'

root :to => 'pages#main'

所以我可以将所有逻辑留在路线上而不是在主控制器中?

谢谢。

So I have my table users and each record has a username field

and I have my root to controller main and action `index

root :to => 'pages#main'

so if go to http://mydomain.com It'll show my main#index page and i have the about and contact pages as well.

but if i go to http://mydomain.com/Mr_Nizzle it shows me the page of the user Mr_Nizzle which is on users#show (just like example) and as well for the other users to show every user's page...

is it right if i go =>

match ':username' => 'users#show'

match 'contact_us' => 'main#contact'

match 'about' => 'main#about'

root :to => 'pages#main'

so i can leave all the logic on route instead of in the main controller?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

太阳哥哥 2024-11-26 20:08:13

我不确定您到底要问什么,但是可能会有所帮助。我认为(至少对于 about 和 contact)正确的语法是 match '/contact_us' => 'main#contact(与 about 相同,但用 about 替换 contact),root :to => 'pages#main' 是正确的(这与匹配 '/' => 'pages#main' 相同)

您对 match ':username' => 有何疑问? '用户#show'?例如,您希望网址为 http://mydomain.com/users_username 吗?其中 users_username 是他们要导航到的用户个人资料的名称?如果是这样,我认为你可以做一些类似的事情 match '/:username' => 'users#show' 但我不完全确定。

请告诉我什么有效、什么无效

I'm not sure exactly what your asking, however this may be of some help. I think (at least for about and contact) the correct syntax is match '/contact_us' => 'main#contact (the same for about but replace contact with about), the root :to => 'pages#main' is correct (that's the same as doing match '/' => 'pages#main')

What are you asking about the match ':username' => 'users#show'? Do you want (for example) a url to be http://mydomain.com/users_username ? Where users_username is the name of the user's profile that they are navigating to? If so, i think you can do something like match '/:username' => 'users#show' but i'm not entirely sure.

Get back to me on what works and what doesn't

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文