railsのcookieのメソッド

# railsに使うメソッド
### cookieの暗号化
>Sets an encrypted cookie value before sending it to the client which
>prevent users from reading and tampering with its value.
>It can be read using the encrypted method `cookies.encrypted[:name]`
```
cookies.encrypted[:discount] = 45
```
https://api.rubyonrails.org/classes/ActionDispatch/Cookies.html#:~:text=%23%20Sets%20an%20encrypted%20cookie%20value%20before%20sending%20it%20to%20the%20client%20which%0A%23%20prevent%20users%20from%20reading%20and%20tampering%20with%20its%20value.%0A%23%20It%20can%20be%20read%20using%20the%20encrypted%20method%20%60cookies.encrypted%5B%3Aname%5D%60%0Acookies.encrypted%5B%3Adiscount%5D%20%3D%2045

>クライアントに送信する前にクッキーの値を自動的に暗号化し、読み取り時には復号化するjarを返す
クッキーがユーザ(または第三者)によって改ざんされていた場合はnil
```
cookies.encrypted[:discount] = 45
#=> Set-Cookie: discount=DIQ7fw==--K3n//8vvnSbGq9dA--7Xh91HfLpwzbj1czhBiwOg==; path=/
cookies.encrypted[:discount] #=> 45
```
https://railsdoc.com/page/cookies_encrypted

暗号化されたものを読み込むには`cookies.encryptedを使う
暗号すれば読まれないようにしているのか。

### 有効期限をつける
> Sets a cookie that expires in 1 hour.
```
cookies[:login] = { value: "XJ-122", expires: 1.hour }
```
>Sets a cookie that expires at a specific time.
```
cookies[:login] = { value: "XJ-122", expires: Time.utc(2020, 10, 15, 5) }
```
https://api.rubyonrails.org/classes/ActionDispatch/Cookies.html#:~:text=%23%20Sets%20a%20cookie%20that%20expires%20in%201%20hour.%0Acookies%5B%3Alogin%5D%20%3D%20%7B%20value%3A%20%22XJ%2D122%22%2C%20expires%3A%201.hour%20%7D%0A%0A%23%20Sets%20a%20cookie%20that%20expires%20at%20a%20specific%20time.%0Acookies%5B%3Alogin%5D%20%3D%20%7B%20value%3A%20%22XJ%2D122%22%2C%20expires%3A%20Time.utc(2020%2C%2010%2C%2015%2C%205)%20%7D

上は時間で、下は日付なのか、有効期限が設定されるといい気がする

>有効期限が設定されたCookieは、Webブラウザを閉じても削除されず、有効期限が来るまでWebブラウザ上に残ります

https://qiita.com/masatom86650860/items/c914c283347e6abeca11#:~:text=%E6%9C%89%E5%8A%B9%E6%9C%9F%E9%99%90%E3%81%8C%E8%A8%AD%E5%AE%9A%E3%81%95%E3%82%8C%E3%81%9FCookie%E3%81%AF%E3%80%81Web%E3%83%96%E3%83%A9%E3%82%A6%E3%82%B6%E3%82%92%E9%96%89%E3%81%98%E3%81%A6%E3%82%82%E5%89%8A%E9%99%A4%E3%81%95%E3%82%8C%E3%81%9A%E3%80%81%E6%9C%89%E5%8A%B9%E6%9C%9F%E9%99%90%E3%81%8C%E6%9D%A5%E3%82%8B%E3%81%BE%E3%81%A7Web%E3%83%96%E3%83%A9%E3%82%A6%E3%82%B6%E4%B8%8A%E3%81%AB%E6%AE%8B%E3%82%8A%E3%81%BE%E3%81%99

### 改竄を防ぐ
> Sets a signed cookie, which prevents users from tampering with its value.
  It can be read using the signed method `cookies.signed[:name]`
```
cookies.signed[:user_id] = current_user.id
```
https://api.rubyonrails.org/classes/ActionDispatch/Cookies.html#:~:text=%23%20Sets%20a%20signed%20cookie%2C%20which%20prevents%20users%20from%20tampering%20with%20its%20value.%0A%23%20It%20can%20be%20read%20using%20the%20signed%20method%20%60cookies.signed%5B%3Aname%5D%60%0Acookies.signed%5B%3Auser_id%5D%20%3D%20current_user.id

### 他のデータ型にするときはシリアライズ化する
>Cookie values are String-based. Other data types need to be serialized.
```
cookies[:lat_lon] = JSON.generate([47.68, -122.37])
```
https://api.rubyonrails.org/classes/ActionDispatch/Cookies.html#:~:text=Cookie%20values%20are%20String%2Dbased.%20Other%20data%20types%20need%20to%20be%20serialized.%0Acookies%5B%3Alat_lon%5D%20%3D%20JSON.generate(%5B47.68%2C%20%2D122.37%5D)

#### シリアライズとは
>シリアライズとは、配列などをそのまま配列として保存しておきたい場合などに使用します。

https://www.sejuku.net/blog/80079#:~:text=%E3%82%B7%E3%83%AA%E3%82%A2%E3%83%A9%E3%82%A4%E3%82%BA%E3%81%A8%E3%81%AF%E3%80%81%E9%85%8D%E5%88%97%E3%81%AA%E3%81%A9%E3%82%92%E3%81%9D%E3%81%AE%E3%81%BE%E3%81%BE%E9%85%8D%E5%88%97%E3%81%A8%E3%81%97%E3%81%A6%E4%BF%9D%E5%AD%98%E3%81%97%E3%81%A6%E3%81%8A%E3%81%8D%E3%81%9F%E3%81%84%E5%A0%B4%E5%90%88%E3%81%AA%E3%81%A9%E3%81%AB%E4%BD%BF%E7%94%A8%E3%81%97%E3%81%BE%E3%81%99%E3%80%82

### ユーザがブラウザを閉じたらcookieも削除される
This cookie will be deleted when the user's browser is closed.
```
cookies[:user_name] = "david"
```
https://api.rubyonrails.org/classes/ActionDispatch/Cookies.html#:~:text=This%20cookie%20will%20be%20deleted%20when%20the%20user%27s%20browser%20is%20closed.%0Acookies%5B%3Auser_name%5D%20%3D%20%22david%22