前提・実現したいこと
Tinderのようなスワイプ機能のあるアプリをRalisにて制作しております。
ユーザーのスワイプの可能回数を1日毎に特定回数に制限したいのですが、
何か方法はありますか?
方法なにかあればご教示ください。。
Posts/index
Ruby
1<script> 2 // スワイプするユーザー数の定数を定義 3 var usersNum = <%= @users.length %> 4 var currentUserIndex = 0; 5 var postReaction = function (user_id, reaction) { 6 $.ajaxSetup({ 7 headers: { 8 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content'), 9 } 10 }); 11 $.ajax({ 12 type: "POST", 13 url: "<%= reactions_path %>", 14 data: { 15 user_id: user_id, 16 reaction: reaction, 17 }, 18 success: function(j_data){ 19 console.log("success") 20 } 21 }); 22 } 23 $("#tinderslide").jTinder({ 24 onDislike: function (item) { 25 currentUserIndex++; 26 checkUserNum(); 27 var user_id = item[0].dataset.user_id 28 postReaction(user_id, 'dislike') 29 }, 30 onLike: function (item) { 31 currentUserIndex++; 32 checkUserNum(); 33 var user_id = item[0].dataset.user_id 34 postReaction(user_id, 'like') 35 }, 36 animationRevertSpeed: 200, 37 animationSpeed: 400, 38 threshold: 1, 39 likeSelector: '.like', 40 dislikeSelector: '.dislike' 41 }); 42 $('.actions .like, .actions .dislike').click(function(e){ 43 e.preventDefault(); 44 $("#tinderslide").jTinder($(this).attr('class')); 45 }); 46 47 function checkUserNum() { 48 // スワイプするユーザー数とスワイプした回数が同じになればaddClassする 49 if (currentUserIndex === usersNum){ 50 $(".noUser").addClass("is-active"); 51 $("#actionBtnArea").addClass("is-none") 52 return; 53 } 54 } 55</script>
photos.controller.erb
Ruby
1class UsersController < ApplicationController 2 # indexアクションを追加する 3 def index 4 @users = User.where.not(id: current_user.id) 5 end 6 7 def show 8 # @userというインスタンス変数を用意する 9 @user = User.find_by(id: params[:id]) 10 end 11end
試したこと
現在、limitで特定回数に制限することはできるものの
「1日ごとに」というところの実装(=limitのカウントを特定期間でリセットする)
ができません。
具体的なソースコードを提示したほうが回答がつきやすいかと思います。
スワイプする度にeventとしてテーブルに1レコード作成しているのであれば、そのレコードのcreated_atを元にwhereで当日分のデータを絞り込み、件数が規定値に達しているかどうかで条件分岐できると思います。
スワイプイベントの回数をどのように管理しているかは最低限ソースコードで示すことで、他の案も出てくると思います。
スワイプした回数を保持している変数は currentUserIndex でしょうか?
それがわからなければどう修正すればよいか回答しようがないのではないかと思っています。
回答者が1から処理を作成するのであれば話は別ですが...
あなたの回答
tips
プレビュー