简体   繁体   English

Rails - 如何临时存储 rails model 实例?

[英]Rails - How do I temporarily store a rails model instance?

I have a rails model instance that needs to be persisted between requests without being saved to the database.我有一个 rails model 实例,需要在请求之间保留而不保存到数据库中。

The small application I'm working on allows customers to use a form to edit the instance parameters, submit the form back to the server, and then view a preview of their changes.我正在开发的小型应用程序允许客户使用表单来编辑实例参数,将表单提交回服务器,然后查看所做更改的预览。 Only when a SAVE button is pressed can the instance data be updated in the database.只有按下 SAVE 按钮时,实例数据才能在数据库中更新。

I was considering storing this data in a session, but some of the instance data could be heavy - eg long full page html strings, BERT serialized objects, etc.我正在考虑将这些数据存储在 session 中,但一些实例数据可能很重——例如长整页 html 字符串、BERT 序列化对象等。

What is the best way to go about this? go 的最佳方式是什么? If there is a general method for this, I'd be glad to hear it.如果有一个通用的方法,我会很高兴听到它。

MORE DETAILS更多细节

Generally, in this type of situation it wouldn't be necessary to persist the data between requests, because the form data will follow us from request to request.通常,在这种情况下,没有必要在请求之间持久化数据,因为表单数据会跟随我们从一个请求到另一个请求。

However, in this situation we have an iframe that is used to preview the recently updated data.但是,在这种情况下,我们有一个 iframe 用于预览最近更新的数据。 The data needs to be persisted.. somehow.. so that the iframe can get it from a separate server request.数据需要以某种方式保留......以便 iframe 可以从单独的服务器请求中获取它。

Generally you shouldn't do this: if you want to make your application more efficient then consider caching the relevant data using a store like memcached/memcache-client. 通常,您不应该这样做:如果您想提高应用程序的效率,请考虑使用诸如memcached / memcache-client之类的存储来缓存相关数据。 Don't use the session. 不要使用会话。

In this instance, you don't even need to store the data, since the form should be passing it all back every time anyway? 在这种情况下,您甚至不需要存储数据,因为表单应该每次都将其全部传回? You can make your preview action build a new object using the data from the form, then render the form out again. 您可以使用表单中的数据使预览操作构建一个新对象,然后再次呈现该表单。

Either store it in the session, or put all the values in hidden fields on the preview page. 可以将其存储在会话中,也可以将所有值放在预览页面的隐藏字段中。 Use the ActiveRecord session store, which serializes the session data to a table in the database, rather than keeping it in memory, or putting it in a cookie (which would probably fail outright due to cookie length limits). 使用ActiveRecord会话存储,它将会话数据序列化到数据库中的表,而不是将其保留在内存中或放入cookie中(由于cookie长度限制,这可能会彻底失败)。

I actually think that a better solution for this is to use hotwire turbo streams and turbo frames我实际上认为更好的解决方案是使用热线涡轮流和涡轮帧

Excerpt taken from turbo documentation摘自turbo 文档

Turbo gives you the speed of a single-page web application without having to write any JavaScript. Turbo accelerates links and form submissions without requiring you to change your server-side generated HTML. It lets you carve up a page into independent frames, which can be lazy-loaded and operate as independent components. Turbo 为您提供单页应用程序的速度 web,而无需编写任何内容 JavaScript。Turbo 可加速链接和表单提交,而无需您更改服务器端生成的 HTML。它可以让您将页面分割成独立的框架,这可以延迟加载并作为独立组件运行。 And finally, helps you make partial page updates using just HTML and a set of CRUD-like container tags.最后,帮助您仅使用 HTML 和一组类似 CRUD 的容器标签进行部分页面更新。 These three techniques reduce the amount of custom JavaScript that many web applications need to write by an order of magnitude.这三种技术将许多 web 应用程序需要编写的自定义 JavaScript 数量减少了一个数量级。 And for the few dynamic bits that are left, you're invited to finish the job with Stimulus.对于剩下的少数动态位,我们邀请您使用 Stimulus 完成这项工作。

On top of accelerating web applications, Turbo was built from the ground-up to form the foundation of hybrid native applications.在加速 web 应用程序之上,Turbo 是从头开始构建的,以形成混合原生应用程序的基础。 Write the navigational shell of your Android or iOS app using the standard platform tooling, then seamlessly fill in features from the web, following native navigation patterns.使用标准平台工具编写 Android 或 iOS 应用程序的导航 shell,然后按照本机导航模式无缝填充 web 中的功能。 Not every mobile screen needs to be written in Swift or Kotlin to feel native.并非每个手机屏幕都需要写成 Swift 或 Kotlin 才能感觉原生。 With Turbo, you spend less time wrangling JSON, waiting on app stores to approve updates, or reimplementing features you've already created in HTML.使用 Turbo,您可以减少争论 JSON、等待应用商店批准更新或重新实现您在 HTML 中创建的功能的时间。

Turbo is a language-agnostic framework written in TypeScript, but this gem builds on top of those basics to make the integration with Rails as smooth as possible. Turbo 是一个在 TypeScript 中编写的 language-agnostic 框架,但这个 gem 建立在这些基础之上,以尽可能顺利地与 Rails 集成。 You can deliver turbo updates via model callbacks over Action Cable, respond to controller actions with native navigation or standard redirects, and render turbo frames with helpers and layout-free responses.您可以通过 Action Cable 上的 model 回调提供 turbo 更新,使用本机导航或标准重定向响应 controller 操作,并使用助手和无布局响应呈现 turbo 帧。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM