如果用戶上傳圖片,您可以將會話變量設置爲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
}
你爲什麼不使用回形針(https://github.com/thoughtbot/paperclip),carrierwave (https://github.com/jnicklas/carrierwave)或蜻蜓(https://github.com/markevans/dragonfly)上傳圖片?這很容易,你不能自己編寫代碼.. – Mattherick 2013-05-11 12:40:12
對不起,我目前使用回形針,但我將圖像保存到數據庫,我只是不知道如何管理數據庫時,我只需要每個用戶/會話一個圖像。 – mlstudent 2013-05-11 14:33:35
你也應該更新你的問題名稱 - Rails-RMagick處理單個圖像 - >也許Rails-Paperclip而不是Rails-RMagick會更好.. – Mattherick 2013-05-13 05:26:20