Rails Session Data - Hash

I have a form on the rails view that passes data to a page that will be a summary page of a shopping cart.

When I send data to the next page, it is transmitted as follows from the output of the console.

"team"=>{"team_name"=>"Joe Blogs", "email"=>" joe@bloggs.com ", "player1"=>" 3", "player2"=>"4", "player3"=>"5"} 

I want to save this data as a session variable, namely as a hash, so if another command is sent to the summary page, I can add it to the session as another hash record. those. team [1], team [2]. Then I can access the command [1] .team_name, etc. And use it accordingly.

In conclusion, I want the user to be able to fill out the form and put it in the basket. Then they can come back and do the same. Finally, they can look at their cart and delete any records they don’t want, empty the trash or send them to the database.

I cannot figure out how to do this or if it is possible.

Any solutions or suggestions on how to implement this?

+5
source share
1 answer

You can easily save the hash in a Rails session.

Example:

 class SomeController < ApplicationController def some_action session[:cart] = {"team_name"=>"Joe Blogs", "email"=>" joe@bloggs.com ", "player1"=>"3", "player2"=>"4", "player3"=>"5"} end end 

But by default, Rails stores sessions in cookies, and the cookie size is limited to only 4 kilobytes of data, so if your hash contains more than a few keys, you will need to use something else for the storage session, such as a database.

To save the session to the database, you can use the activerecord-session_store gem.

+3
source

Source: https://habr.com/ru/post/1236010/


All Articles