Rails Detect if the user first visits

I am trying to get a user to fill out a questionnaire if he visits the site for the first time.

My controllers are configured as follows:

class MainController < BaseController end class BaseController < ApplicationController before_filter :first_time_visiting? end class ApplicationController < ActionController::Base def first_time_visiting? if session[:first_time].nil? session[:first_time] = 1 redirect_to questionnaire_path unless current_user end end end 

When I close the browser and open it again, I am always redirected to the questionnaire.

+7
source share
3 answers

You must set a cookie in the browser for this user to allow detection later, that is, after the user closes the browser. Setting and reading cookies on rails is easy. Make documentation for use in some examples. http://api.rubyonrails.org/classes/ActionDispatch/Cookies.html

+6
source

Having stumbled upon this question, my solution is just jQuery and it involves setting up localStorage (make sure you have a polyfill). Hide the item you want to show only once.

 $(function() { if ( localStorage.getItem('visited') ) { return; } var $el = $('.only-show-on-first-visit'); $el.slideDown(800); localStorage.setItem('visited', true); $el.find('.close').click(function() { $el.slideUp(); }); }); // polyfill if (!('localStorage' in window)) { window.localStorage = { _data : {}, setItem : function(id, val) { return this._data[id] = String(val); }, getItem : function(id) { return this._data.hasOwnProperty(id) ? this._data[id] : undefined; }, removeItem : function(id) { return delete this._data[id]; }, clear : function() { return this._data = {}; } }; } 
+1
source

Add the last field to the user table during login. When is it nil do foo and! Nil do bar.

0
source

All Articles