Rails - 如何临时存储 Rails 模型实例? [英] Rails - How do I temporarily store a rails model instance?

查看:46
本文介绍了Rails - 如何临时存储 Rails 模型实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 rails 模型实例,它需要在请求之间持久化而不保存到数据库中.

I have a rails model instance that needs to be persisted between requests without being saved to the database.

我正在开发的小型应用程序允许客户使用表单来编辑实例参数,将表单提交回服务器,然后查看更改的预览.只有按下SAVE按钮才能更新数据库中的实例数据.

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.

我正在考虑将这些数据存储在会话中,但某些实例数据可能很重 - 例如长整页 html 字符串、BERT 序列化对象等

I was considering storing this data in a session, but some of the instance data could be heavy - e.g. long full page html strings, BERT serialized objects, etc.

解决这个问题的最佳方法是什么?如果有一个通用的方法,我会很高兴听到它.

What is the best way to go about this? If there is a general method for this, I'd be glad to hear it.

通常,在这种情况下,不需要在请求之间持久保存数据,因为表单数据将跟随我们从一个请求到另一个请求.

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.

但是,在这种情况下,我们有一个 iframe 用于预览最近更新的数据.数据需要持久化......以某种方式......以便 iframe 可以从单独的服务器请求中获取它.

However, in this situation we have an iframe that is used to preview the recently updated data. The data needs to be persisted.. somehow.. so that the iframe can get it from a separate server request.

推荐答案

通常你不应该这样做:如果你想让你的应用程序更高效,那么考虑使用像 memcached/memcache-client 这样的存储来缓存相关数据.不要使用会话.

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. 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.

这篇关于Rails - 如何临时存储 Rails 模型实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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