Using Ruby on Rails 5.0.3 .
I want to set some value in cookie using JavaScript (when button clicked), and get it in Rails.
I know how to access session in Rails, which is session[:some_key]
or cookies
.
But I don't know how to do in JavaScript. (it must be able to accessed from Rails session
or cookies
.)
How can I do it in JS?
Or any other ways to save some value in JS, and get it later in Rails ?
Unfortunately, you can't modify Rails session from the client side, even if it stored in cookies. Because the rails session is encrypted. As a solution, you can use regular cookies, not the Rails session.
Check http://api.rubyonrails.org/v5.1/classes/ActionDispatch/Cookies.html https://www.w3schools.com/js/js_cookies.asp for more information.
The cookie for the rails app is just some hashed string to ID the server cookie, which has the data.
What they said, use Ajax to some controller method you write to update your necessary info into the rails cookie.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With