Rails - How to set persistent randomized cookies? -
i want set cookie system on app, everytime new user arrive, gets cookie random number (token). want user have token every time user save item, token saved in item database, app can check how many items user saved.
i tried write application controller : if there no cookies stocked, cookie given user random number. if don't add condition, cookie number randomly change every page reload.
this application controller :
class applicationcontroller < actioncontroller::base # prevent csrf attacks raising exception. # apis, may want use :null_session instead. protect_from_forgery with: :exception before_action :set_cookies private def set_cookies if !(cookies[:token].blank?) cookies[:token] = { value: securerandom.random_number, domain: "mydomain" } end end end
with this, seems cookie storage never works. tried different ways of writing condition. nothing works.
i check if there cookie writing in view :
<%= cookies[:token].to_s %>
when add condition, space empty.
what obvious thing missing here ?
edit: answer doesn't seem work :/
you have negation in condition:
if !(cookies[:token].blank?) cookies[:token] = ...
so token set if non-blank
Comments
Post a Comment