Ruby on Rails-将ID参数添加到所有路线 [英] Ruby on Rails - add ID parameter to all routes

查看:78
本文介绍了Ruby on Rails-将ID参数添加到所有路线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道在Rails路由中实现以下功能的最佳方法是什么:

I am wondering what is the best way to implement the following functionality in the Rails routing:

方案:用户注册帐户的网站=> accountID(帐户)成为网站内的主要实体。

Scenario: an website where users sign up for accounts => accountID (Account) becomes the main entity within the website.

示例: https://basecamp.com/:ID/ -将授权用户带到Basecamp仪表板。从这里开始,所有URL都包含:accountID,如 https://basecamp.com/:ID/projects-列出该帐户下的所有项目。

Example: https://basecamp.com/:ID/ - takes the authorized users to the Basecamp dashboard. From here all the URLs contain the :accountID as in https://basecamp.com/:ID/projects - list all the projects under the account.

非常感谢!

推荐答案

使用路由前缀

scope ":account_id" do
  resources :projects
  ...
end

这将始终为您提供参数范围内定义的每个资源控制器上的[:account_id]。

This will always give you params[:account_id] on each resource controller that is defined within the scope.

在此处了解更多信息: http://guides.rubyonrails.org/routing.html#prefixing-the-named-route-helpers

Read more here: http://guides.rubyonrails.org/routing.html#prefixing-the-named-route-helpers

更新:以下是整个示例,其中包括仪表板默认路线

Update: Here's an entire example including your "dashboard" default route

scope ":account_id" do
  root :to => "dashboard#index"  # http://example.com/12323/
  resources :projects            # http://example.com/12323/projects
  resources :todos               # http://example.com/12323/todos
  ...
end

这篇关于Ruby on Rails-将ID参数添加到所有路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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