使用Rails 3 w / devise中的用户名绘制路线 [英] Drawing routes using user name in Rails 3 w/ devise

查看:120
本文介绍了使用Rails 3 w / devise中的用户名绘制路线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于用户管理的用户与事物之间存在一对多的关系,我的目标是绘制如下平静的路线:

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路线,但我不知道如何将它应用于通过设计创建和管理的通用用户模型。

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 ":username", :as => "user" do
  resources :things
end

将此与code> to_param 在用户型号上:

Combine this with to_param on the user model:

def to_param
  username
end

你会有路由,如 / username / things 。注意,用户名不应包含任何点,斜线或标准URI字符。您可能希望在用户名的末尾夹上参数化。确保。

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 w / devise中的用户名绘制路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆