Ruby on Rails 中的 URI 管理和导航

发布于 2024-10-18 09:13:30 字数 706 浏览 0 评论 0原文

我想在基于 URI 的 Rails 中实现特殊路由(我使用带有 Mongoid 和 devise 的 Rails 3.0.4)。假设我的用户登录,登录后我想将他重定向到私人区域,例如 http://www.mysite。 com/site1。许多用户可能属于 site1,许多用户可能属于 site2...一个网站的用户无权查看另一个网站。今天登录后重定向很好,但我对如何实现 siteX 感到困惑(每个站点都有自己的数据)。我有以下路线:

match '/:site_name' => 'site#index', :constraints => { :site_name => /'a pattern'/ }
  resources :sites do
end

因为我需要坚持这种 URI 格式,所以我应该将所有其他资源嵌套在 :sites 中吗?例如,如果我想显示站点 2 的订单 1,则 URL 应类似于 http://www。 mysite.com/site2/order/1。我无法将资源名称“sites”放入 URI 中,因为它直接以标识符开头。还有另一种方法可以做到这一点,最好的做法是什么?希望我的解释有意义。

非常感谢您的帮助! 特德

I'd like to implement a special routing in rails based on the URI (I'm using rails 3.0.4 with Mongoid and devise). Let's say my user logins, once signin I want to redirect him to a private area for example http://www.mysite.com/site1. Many users could belong to site1, many to site2... users of one site are not authorized to see another site. Today the redirection is fine after sign in, but I'm confused on how I should implement siteX (each site has its own data). I have the following route:

match '/:site_name' => 'site#index', :constraints => { :site_name => /'a pattern'/ }
  resources :sites do
end

Because I need to stick to this URI format should I nest all my other resources inside :sites? For example if I want to display order 1 of site 2 the URL should look like http://www.mysite.com/site2/order/1. I can't put the resource's name "sites" in the URI as it starts directly with the identifier. Is there another way of doing this, what would be the best pratices? Hope my explanations make sense.

Thanks a lot for all your help!
Ted

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

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

发布评论

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

评论(2

萌能量女王 2024-10-25 09:13:30

我建议您放弃“子目录”的想法。使用 link_to 和其他帮助程序时,您会遇到(并非不可克服的)困难。

如果您的情况可行,我会设置子域(例如,site1.mysite.com)

I recommend you scrap the idea of "subdirectories". You'll have (not insurmountable) difficulties with link_to and the other helpers.

I would setup subdomains (a la, site1.mysite.com) if that's possible for your situation

池予 2024-10-25 09:13:30

url.com/site_name 有点疯狂。

如果只有一个用户可以属于一个站点,那么从用户的角度考虑并使用resource而不是resources

例如,url.com/orders 将全部是 current_user.orders,因为 current_user 有一个网站(或者是一个网站的成员)。

如果您需要特定于站点的导航,请从 /site_name 获取本质上公开的特定于站点的详细信息。例如,url.com/site_name/pricing

如果您确实想将网站分解为 /site_name 特定路由,则可以将其分解为子域。您甚至可以尝试使用 sudomain devise 来开始。

Doing url.com/site_name is kind of nuts.

If only one user can belong to a site, take it from the user perspective then and use resource and not resources.

E.g., url.com/orders would be all current_user.orders, since current_user has_one site (or is a member of one site).

If you need site specific navigation, then draw from /site_name for site specific detail that is public in nature. E.g., url.com/site_name/pricing

If you really want to break your site down into /site_name specific routes, then through that into a subdomain. You can even try using sudomain devise to get you started.

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