Rails 检测用户是否第一次访问 [英] Rails Detect If User's Very First Visit
问题描述
如果用户是第一次访问该网站,我会尝试让他们填写一份调查问卷.
I'm trying to make the user fill out a questionnaire if it is their first time visiting the site.
我的控制器设置如下:
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 re-open it though, I always get redirected to the questionnaire.
推荐答案
您必须在浏览器中为该用户设置 cookie,以便稍后(即在用户关闭浏览器后)进行检测.在 Rails 中设置和读取 cookie 很容易.查看文档以了解一些示例用法.http://api.rubyonrails.org/classes/ActionDispatch/Cookies.html
You have to set a cookie in the browser for that user in order to allow detection at a later time, i.e. after the user closes the browser. Setting and reading cookies in rails is easy. Checkout the documentation for some example usage. http://api.rubyonrails.org/classes/ActionDispatch/Cookies.html
这篇关于Rails 检测用户是否第一次访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!