鉴于使用设计管理的用户与“事物”之间存在一对多关系,我的目标是绘制 Restful 路线,例如:
http://host/username
http://host/username/things
http://host/username/things/1
...
我知道 Rails 路由中的 nested resources,但我不知道如何将它应用于通过设计创建和管理的通用用户模型。
最佳答案
您可以为此使用 scope
:
scope ":username", :as => "user" do
resources :things
end
将此与用户模型上的
to_param
结合使用:def to_param
username
end
并且您将拥有诸如
/username/things
之类的路线。但请注意,用户名不应包含任何点、正斜杠或标准 URI 字符。您可能想在 parameterize
的末尾添加一个 username
以确保。
关于ruby-on-rails - 在 Rails 3 w/devise 中使用用户名绘制路线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4514533/