如何在 Rails 3 中为每个用户提供不同的视图?
所以我有我的表users
,每条记录都有一个username
字段
,我有控制器main
的根和操作`index,
root :to => 'pages#main'
所以如果转到http://mydomain.com
它将显示我的 main#index
页面,我有 about
和 contact
页面也是如此。
但如果我访问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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定您到底要问什么,但是这可能会有所帮助。我认为(至少对于 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), theroot :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 likematch '/:username' => 'users#show'
but i'm not entirely sure.Get back to me on what works and what doesn't