Any user can 'like' photos on my site. But if he presses the button many times (10 for example), the 10 post requests will be sent to the server. I tried to solve with the help of sessions. I thought this code would take likes only each 5 seconds. But it doesnt! There are my action and before_filter:
photo.create_like if session[:voted].nil?
session[:voted] = Time.now.to_a.first(3).reverse.join
a = Time.now.to_a.first(3).reverse.join
b = a.to_i - session[:voted].to_i
session[:voted] = nil if b >= 5
Best How To :
You can't do this with sessions (at least not with the default session store): by default rails stores the session in a cookie.
Cookies are sent by the browser as part of the request, and the response from your server can optionally update them. If you click on your like button several times in quick succession then you'll fire off several requests, each containing cookie data representing the current state of the session. Your response updates the session, but it's too late for the requests that have already been sent - their session data has been already been sent to the server and won't include any changes made by the responses.