我正在我的網頁上設置一個簡單的調查。 我想添加一個before_filter,以便同一個人不能多次參與調查。如何爲我們在第一次過濾之前過濾器中的永久cookie過濾器
我的想法是
1)創建和提交時一個remember_token保存每個調查。
2)創建一個基於記得令牌的cookie被放置在提交的瀏覽器
3)每次有訪問的頁面,使用過濾器之前,以確保他們沒有一個匹配的cookie數據庫中的一項調查。
我放在一起下面,但由於某種原因,它會自動重定向到thanks_path,無論我是否有記憶令牌?
它爲什麼這樣做?我是否錯誤地使用會話cookie?
我surveys_controller
是如下
before_filter :new_visitor, only: [:new, :create]
def new
#this is the survey form
@survey = Survey.new
end
def create
#this submits the survey and creates a cookie on the client's browser
@survey = Survey.new(params[:survey])
if @survey.save
cookies.permanent[:remember_token] = @survey.remember_token
redirect_to thanks_path
else
render action: "new"
end
end
def thanks
#blank page that just says, "thanks for taking the survey!"
end
def new_visitor
# if a browser has a survey cookie, redirect to thanks page
unless Survey.find_by_remember_token(cookies[:remember_token]).nil?
redirect_to thanks_path
end
end
我創造我Survey
模型記得令牌。
class Survey < ActiveRecord::Base
before_save :create_remember_token
def create_remember_token
self.remember_token = SecureRandom.urlsafe_base64
end
end
是的,好像是這個問題,謝謝! – umezo 2013-04-04 23:09:26