如果用户上传图像,您可以将会话变量设置为 true,并检查您的上传控制器是否设置了会话变量。取决于您是否允许用户上传图像。您可以将会话存储设置为 db,进一步您可以定义会话保存多长时间的范围。
控制器:
def new
@upload = YourUploadModel.new
session[:image_uploaded] ||= true
end
def create
if session[:image_uploaded] && session[:image_uploaded] == true
redirect_to root_path, :notice => "Already uploaded an image today!"
else
# create your upload..
end
end
应用程序/配置/初始化程序/session_store.rb:
# Use the database for sessions instead of the cookie-based default,
# which shouldn't be used to store highly confidential information
# (create the session table with "rails generate session_migration")
YourAppname::Application.config.session_store :active_record_store, {
expire_after: 1.days
}