This code works... but feel it's not the Rails way and could be more efficient.
How would you optimize this snippet? especially the checking for @post.likes_count.nil?
How would you implement throttling? Somebody can click the LIKE button multiple times, and it would generate multiple emails to the original poster. Wouldn't want that.
def likepost # look for post by id @post = Post.find_by(id: params[:id]) # TO DO: we need throttling here... in case user multiple clicks the LIKE button, and inundate # original poster with multiple email notifications if @post # if found, increment likes_count counter, then save if @post.likes_count.nil? @post.likes_count = 1 else @post.likes_count += 1 end @post.save # TO DO: send email notification to original poster # end # update the html page via Ajax respond_to do |format| format.js end end