Using sessions, cookies, ...
user_id
, preferred language)sessions_controller
#sessions_controller.rb
def create
session[:current_user_id] = @user.id
end
#can be read in another controller, for example users_controller.rb
def index
current_user = User.find_by_id(session[:current_user_id])
end
When you send a request (and the link contains parameters) to your website,
you are able to extract those sent parameters in the Rails param hash param[:user_id]
Rails then saves the params in a session cookie!
session_my_app
config/initializers/session_store.rb
in the following LOC:
Rails.application.config.session_store :cookie_store, key: '_session_my_app'
Rails encrypts (for security reasons) and signs (to prevent users from tampering it) the session cookie
secret_key
(found in config/secrets.yml
):production:
secret_key_base = <% ENV["SECRET_KEY_BASE"] %>
Rails.application.key_generator
config/initializers/cookies_serializer.rb
:
Rails.application.config.action_dispatch.cookies_serializer = :json
Rails then sends the encrypted cookie back to the browser, browser stores the cookie.
Rails can change the data in the cookie, this will overwrite the cookie and still be able to send the cookie back and forth between server and browser.