在 Rails 中跨请求记住多个参数的正确方法 [英] Proper way to remember multiple parameters across requests in Rails

查看:40
本文介绍了在 Rails 中跨请求记住多个参数的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序有一个主"页面,大部分操作都在其中发生:有用于过滤的标签和(分页)表中的结果列表,以及在购物车"中选择部分或全部结果的可能性.

My application feature a "main" page where most of the action happens: There are tags for filtering and a list of results in a (paginated) table, plus the possibility to select some or all results in a "shopping cart".

此页面必须跟踪很多事情:选择了哪些标签、选择了哪些项目、结果表如何排序以及它在哪个页面上.一切都必须坚持,所以如果我选择一个新标签,页面必须部分重新加载但记住所有内容(排序、选择的内容).

This page has to keep track of a whole lot of things: what tags are selected, what items are selected, and how the result table is sorted and what page it's on. Everything has to persist, so if I select a new tag, the page must partially reload but remember everything (sorting, what's selected).

现在我正在使用参数处理所有内容,并且对于在页面上执行的每个操作,所有链接(选择标签/项目、更改页面、排序表)都会更新以包含以前的参数 + 相关的新增内容.这显然有效,但感觉有点低效,因为我必须重新加载比我想要的更多的页面.这种情况一般怎么处理?我在谷歌上根本找不到那么多信息,但感觉这不是一个特别罕见的案例.

Right now I'm handling everything with parameters, and for each action taken on the page, all links (select a tag/item, change page, sort table) are updated to include previous parameters + the relevant new addition. This works, obviously, but it feels kind of inefficient, as I have to reload more of the page than I want to. How is this situation normally handled? I can't find that much info on google at all, but it doesn't feel like a particularly uncommon case.

tl;dr:如何最好地确保所有链接(指向同一页面)始终包含以前选择的所有内容 + 新操作.有很多链接(每个标签一个选择/取消选择,每个结果项目一个选择/取消选择,每个排序选项一个,每页一个)

tl;dr: How to best make sure all links (to the same page) always include everything previously selected + the new action. There are a lot of links (one per tag to select/deselect, one per result item to select/deselect, one per sort option, one per page)

推荐答案

有五种方法可以做到:

你提到了这一点.我从来没想过这个,因为太麻烦了.无论如何,它仍然是非常简单的情况的解决方案.

You mentioned this. I never think of this as it's too troublesome. Anyway it's still a solution for very simple case.

将设置保存到 cookie 并在控制器中读取 cookie 以安排布局设置.

Save the settings to a cookie and read the cookie in controller to arrange layout settings.

类似于 cookie,但允许更多空间.

Similar to cookie but allows more space.

如果您使用 ActiveRecord 来保存会话,这可能是纯页面加载的最佳解决方案.将用户首选项保存到会话中并在下一个布局中加载.

If you are using ActiveRecord to save session, this could be the best solution for pure pages loading. Save the user preferences into session and load it in next layout.

这是 IMO 的最佳解决方案.使用 Ajax 刷新/检索您需要的更改,而不是加载整个页面.结合上述方法,用户甚至可以继续他上次的喜好.这是最强大的,应该适用于您的情况,它看起来像一个网络应用程序而不是一个网站.

This is the best solution IMO. Instead of whole page loading, use Ajax to refresh/retrieve changes you need. Using together with above method, a user can even continue his last preferences. This is the most powerful and should be applicable to your case which looks like a web app than a website.

这篇关于在 Rails 中跨请求记住多个参数的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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