问题描述
鉴于使用设计管理的用户和事物"之间存在一对多关系,我的目标是绘制宁静的路线,例如:
Given a one to many relationship between a user managed with devise and a "thing", my goal is to draw restful routes like:
http://host/username
http://host/username/things
http://host/username/things/1
...
我知道 Rails 路由中有嵌套资源,但我不能t 弄清楚如何将其应用于通过设计创建和管理的通用用户模型.
I am aware of nested resources in Rails routes, but I can't figure out how to apply it to a generic User model created and managed via devise.
推荐答案
您可以使用 scope
:
scope ":username", :as => "user" do
resources :things
end
将其与用户模型上的 to_param
结合起来:
Combine this with to_param
on the user model:
def to_param
username
end
您将拥有诸如 /username/things
之类的路由.但请注意,用户名不应包含任何点、正斜杠或标准 URI 字符.您可能需要在 username
的末尾添加一个 parameterize
以确保.
And you'll have routes such as /username/things
. Be careful though, the username shouldn't contain any dots, forward slashes or standard URI characters. You may want to chuck a parameterize
on the end of username
to make sure.
这篇关于在带有设计的 Rails 3 中使用用户名绘制路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!