Rails - 为每个打开的选项卡/窗口存储唯一数据
我有一个应用程序,它具有不同的数据集,具体取决于用户当前选择的公司(侧边栏上的下拉框当前用于设置会话变量)。
我的客户表示希望能够从一个浏览器同时处理多个不同的数据集。因此,会议不再有效。
谷歌搜索似乎暗示随着每个请求获取或发布数据就是这样,这是我的第一个猜测。有没有更好/更简单/rails 的方法来实现这一目标?
I have an application that has different data sets depending on which company the user has currently selected (dropdown box on sidebar currently used to set a session variable).
My client has expressed a desire to have the ability to work on multiple different data sets from a single browser simultaneously. Hence, sessions no longer cut it.
Googling seems to imply get or post data along with every request is the way, which was my first guess. Is there a better/easier/rails way to achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您在这里有几个选项,但正如您所指出的,
session
系统不适合您,因为它在同一浏览器的所有实例中都是全局的。标准方法是在 URL 中添加一些内容来标识执行的上下文。这可以像
/companyx/users
这样的前缀一样简单,而不是使用/users
来获取公司名称并将其用作范围。通常,您可以通过拥有一个为您完成此工作的控制器基类来完成此操作,然后从该基类继承所有其他控制器,这些控制器将以相同的方式受到影响。另一种方法是将公司标识部分从 URL 移至主机名。这在软件即服务提供商中很常见,因为它使应用程序的分片变得更加容易。您将拥有
companyx.myapp.com/users
,而不是myapp.com/companyx/users
。这样做的优点是保留现有的 URL 结构,并且当您有大量数据时,您可以按客户将应用程序分区到不同的数据库中,而不会很麻烦。您通过使用 GET 令牌或 POST 字段标记所有 URL 找到的答案不会很好地工作。首先,它很混乱,其次,每个链接都是 POST 的网站使用起来非常烦人,因为它使得使用后退按钮导航或强制重新加载变得很麻烦。它之所以被使用,是因为开箱即用的 PHP 和 ASP 没有支持路线,所以人们不得不凑合着用。
You have a few options here, but as you point out, the
session
system won't work for you since it is global across all instances of the same browser.The standard approach is to add something to the URL that identifies the context in which to execute. This could be as simple as a prefix like
/companyx/users
instead of/users
where you're fetching the company slug and using that as a scope. Generally you do this by having a controller base class that does this work for you, then inherit from that for all other controllers that will be affected the same way.Another approach is to move the company identifying component from the URL to the host name. This is common amongst software-as-a-service providers because it makes sharding your application much easier. Instead of
myapp.com/companyx/users
you'd havecompanyx.myapp.com/users
. This has the advantage of preserving the existing URL structure, and when you have large amounts of data, you can partition your app by customer into different databases without a lot of headache.The answer you found with tagging all the URLs using a GET token or a POST field is not going to work very well. For one, it's messy, and secondly, a site with every link being a POST is very annoying to work with as it makes navigating with the back-button or forcing a reload troublesome. The reason it has seen use is because out of the box PHP and ASP do not have support routes, so people have had to make do.
您可以创建临时数据库表,或使用键值数据库并在其中存储所需的所有数据。 uniq 键可以用作窗口 ID。此外,您必须将此窗口 ID 添加到每个链接。因此,您可以从数据库中接收每个浏览器选项卡的相应数据,并将其存储在会话、对象中……
如果您有一个对象,比如说@data,您可以使用 Marshal.dump 将其存储在数据库中,使用 Marshal.load 将其取回。
You can create a temporary database table, or use a key-value database and store all data you need in it. The uniq key can be used as a window id. Furthermore, you have to add this window id to each link. So you can receive the corresponding data for each browser tab out of the database and store it in the session, object,...
If you have an object, lets say @data, you can store it in the database using Marshal.dump and get it back with Marshal.load.