简体   繁体   中英

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.

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.

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.

What is the best way to go about this? 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. The data needs to be persisted.. somehow.. so that the iframe can get it from a separate server request.

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.

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

I actually think that a better solution for this is to use hotwire turbo streams and turbo frames

Excerpt taken from turbo documentation

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. And finally, helps you make partial page updates using just HTML and a set of CRUD-like container tags. These three techniques reduce the amount of custom JavaScript that many web applications need to write by an order of magnitude. And for the few dynamic bits that are left, you're invited to finish the job with Stimulus.

On top of accelerating web applications, Turbo was built from the ground-up to form the foundation of hybrid native applications. 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. Not every mobile screen needs to be written in Swift or Kotlin to feel native. 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 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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